]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/staging/brcm80211/brcmfmac/dhd_common.c
OMAP: DSS2: Introduce omap_channel as an omap_dss_device parameter, add new overlay...
[mv-sheeva.git] / drivers / staging / brcm80211 / brcmfmac / dhd_common.c
1 /*
2  * Copyright (c) 2010 Broadcom Corporation
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 #include <linux/kernel.h>
17 #include <linux/string.h>
18 #include <bcmdefs.h>
19 #include <osl.h>
20 #include <bcmutils.h>
21 #include <bcmendian.h>
22 #include <dngl_stats.h>
23 #include <dhd.h>
24 #include <dhd_bus.h>
25 #include <dhd_proto.h>
26 #include <dhd_dbg.h>
27 #include <msgtrace.h>
28 #include <wlioctl.h>
29
30 int dhd_msg_level;
31 char fw_path[MOD_PARAM_PATHLEN];
32 char nv_path[MOD_PARAM_PATHLEN];
33
34 /* Last connection success/failure status */
35 u32 dhd_conn_event;
36 u32 dhd_conn_status;
37 u32 dhd_conn_reason;
38
39 #define htod32(i) i
40 #define htod16(i) i
41 #define dtoh32(i) i
42 #define dtoh16(i) i
43
44 extern int dhdcdc_set_ioctl(dhd_pub_t *dhd, int ifidx, uint cmd, void *buf,
45                             uint len);
46 extern void dhd_ind_scan_confirm(void *h, bool status);
47 extern int dhd_wl_ioctl(dhd_pub_t *dhd, uint cmd, char *buf, uint buflen);
48 void dhd_iscan_lock(void);
49 void dhd_iscan_unlock(void);
50
51 /* Packet alignment for most efficient SDIO (can change based on platform) */
52 #ifndef DHD_SDALIGN
53 #define DHD_SDALIGN     32
54 #endif
55 #if !ISPOWEROF2(DHD_SDALIGN)
56 #error DHD_SDALIGN is not a power of 2!
57 #endif
58
59 #ifdef DHD_DEBUG
60 #define EPI_VERSION_STR         "4.218.248.5"
61 const char dhd_version[] =
62 "Dongle Host Driver, version " EPI_VERSION_STR "\nCompiled on " __DATE__
63 " at " __TIME__;
64 #else
65 const char dhd_version[] = "Dongle Host Driver, version " EPI_VERSION_STR;
66 #endif
67
68 void dhd_set_timer(void *bus, uint wdtick);
69
70 /* IOVar table */
71 enum {
72         IOV_VERSION = 1,
73         IOV_MSGLEVEL,
74         IOV_BCMERRORSTR,
75         IOV_BCMERROR,
76         IOV_WDTICK,
77         IOV_DUMP,
78 #ifdef DHD_DEBUG
79         IOV_CONS,
80         IOV_DCONSOLE_POLL,
81 #endif
82         IOV_CLEARCOUNTS,
83         IOV_LOGDUMP,
84         IOV_LOGCAL,
85         IOV_LOGSTAMP,
86         IOV_GPIOOB,
87         IOV_IOCTLTIMEOUT,
88         IOV_LAST
89 };
90
91 const bcm_iovar_t dhd_iovars[] = {
92         {"version", IOV_VERSION, 0, IOVT_BUFFER, sizeof(dhd_version)}
93         ,
94 #ifdef DHD_DEBUG
95         {"msglevel", IOV_MSGLEVEL, 0, IOVT_UINT32, 0}
96         ,
97 #endif                          /* DHD_DEBUG */
98         {"bcmerrorstr", IOV_BCMERRORSTR, 0, IOVT_BUFFER, BCME_STRLEN}
99         ,
100         {"bcmerror", IOV_BCMERROR, 0, IOVT_INT8, 0}
101         ,
102         {"wdtick", IOV_WDTICK, 0, IOVT_UINT32, 0}
103         ,
104         {"dump", IOV_DUMP, 0, IOVT_BUFFER, DHD_IOCTL_MAXLEN}
105         ,
106 #ifdef DHD_DEBUG
107         {"dconpoll", IOV_DCONSOLE_POLL, 0, IOVT_UINT32, 0}
108         ,
109         {"cons", IOV_CONS, 0, IOVT_BUFFER, 0}
110         ,
111 #endif
112         {"clearcounts", IOV_CLEARCOUNTS, 0, IOVT_VOID, 0}
113         ,
114         {"gpioob", IOV_GPIOOB, 0, IOVT_UINT32, 0}
115         ,
116         {"ioctl_timeout", IOV_IOCTLTIMEOUT, 0, IOVT_UINT32, 0}
117         ,
118         {NULL, 0, 0, 0, 0}
119 };
120
121 void dhd_common_init(void)
122 {
123         /* Init global variables at run-time, not as part of the declaration.
124          * This is required to support init/de-init of the driver.
125          * Initialization
126          * of globals as part of the declaration results in non-deterministic
127          * behaviour since the value of the globals may be different on the
128          * first time that the driver is initialized vs subsequent
129          * initializations.
130          */
131         dhd_msg_level = DHD_ERROR_VAL;
132 #ifdef CONFIG_BCM4329_FW_PATH
133         strncpy(fw_path, CONFIG_BCM4329_FW_PATH, MOD_PARAM_PATHLEN - 1);
134 #else
135         fw_path[0] = '\0';
136 #endif
137 #ifdef CONFIG_BCM4329_NVRAM_PATH
138         strncpy(nv_path, CONFIG_BCM4329_NVRAM_PATH, MOD_PARAM_PATHLEN - 1);
139 #else
140         nv_path[0] = '\0';
141 #endif
142 }
143
144 static int dhd_dump(dhd_pub_t *dhdp, char *buf, int buflen)
145 {
146         struct bcmstrbuf b;
147         struct bcmstrbuf *strbuf = &b;
148
149         bcm_binit(strbuf, buf, buflen);
150
151         /* Base DHD info */
152         bcm_bprintf(strbuf, "%s\n", dhd_version);
153         bcm_bprintf(strbuf, "\n");
154         bcm_bprintf(strbuf, "pub.up %d pub.txoff %d pub.busstate %d\n",
155                     dhdp->up, dhdp->txoff, dhdp->busstate);
156         bcm_bprintf(strbuf, "pub.hdrlen %d pub.maxctl %d pub.rxsz %d\n",
157                     dhdp->hdrlen, dhdp->maxctl, dhdp->rxsz);
158         bcm_bprintf(strbuf, "pub.iswl %d pub.drv_version %ld pub.mac %pM\n",
159                     dhdp->iswl, dhdp->drv_version, &dhdp->mac);
160         bcm_bprintf(strbuf, "pub.bcmerror %d tickcnt %d\n", dhdp->bcmerror,
161                     dhdp->tickcnt);
162
163         bcm_bprintf(strbuf, "dongle stats:\n");
164         bcm_bprintf(strbuf,
165                     "tx_packets %ld tx_bytes %ld tx_errors %ld tx_dropped %ld\n",
166                     dhdp->dstats.tx_packets, dhdp->dstats.tx_bytes,
167                     dhdp->dstats.tx_errors, dhdp->dstats.tx_dropped);
168         bcm_bprintf(strbuf,
169                     "rx_packets %ld rx_bytes %ld rx_errors %ld rx_dropped %ld\n",
170                     dhdp->dstats.rx_packets, dhdp->dstats.rx_bytes,
171                     dhdp->dstats.rx_errors, dhdp->dstats.rx_dropped);
172         bcm_bprintf(strbuf, "multicast %ld\n", dhdp->dstats.multicast);
173
174         bcm_bprintf(strbuf, "bus stats:\n");
175         bcm_bprintf(strbuf, "tx_packets %ld tx_multicast %ld tx_errors %ld\n",
176                     dhdp->tx_packets, dhdp->tx_multicast, dhdp->tx_errors);
177         bcm_bprintf(strbuf, "tx_ctlpkts %ld tx_ctlerrs %ld\n",
178                     dhdp->tx_ctlpkts, dhdp->tx_ctlerrs);
179         bcm_bprintf(strbuf, "rx_packets %ld rx_multicast %ld rx_errors %ld\n",
180                     dhdp->rx_packets, dhdp->rx_multicast, dhdp->rx_errors);
181         bcm_bprintf(strbuf,
182                     "rx_ctlpkts %ld rx_ctlerrs %ld rx_dropped %ld rx_flushed %ld\n",
183                     dhdp->rx_ctlpkts, dhdp->rx_ctlerrs, dhdp->rx_dropped,
184                     dhdp->rx_flushed);
185         bcm_bprintf(strbuf,
186                     "rx_readahead_cnt %ld tx_realloc %ld fc_packets %ld\n",
187                     dhdp->rx_readahead_cnt, dhdp->tx_realloc, dhdp->fc_packets);
188         bcm_bprintf(strbuf, "wd_dpc_sched %ld\n", dhdp->wd_dpc_sched);
189         bcm_bprintf(strbuf, "\n");
190
191         /* Add any prot info */
192         dhd_prot_dump(dhdp, strbuf);
193         bcm_bprintf(strbuf, "\n");
194
195         /* Add any bus info */
196         dhd_bus_dump(dhdp, strbuf);
197
198         return !strbuf->size ? BCME_BUFTOOSHORT : 0;
199 }
200
201 static int
202 dhd_doiovar(dhd_pub_t *dhd_pub, const bcm_iovar_t *vi, u32 actionid,
203             const char *name, void *params, int plen, void *arg, int len,
204             int val_size)
205 {
206         int bcmerror = 0;
207         s32 int_val = 0;
208
209         DHD_TRACE(("%s: Enter\n", __func__));
210
211         bcmerror = bcm_iovar_lencheck(vi, arg, len, IOV_ISSET(actionid));
212         if (bcmerror != 0)
213                 goto exit;
214
215         if (plen >= (int)sizeof(int_val))
216                 bcopy(params, &int_val, sizeof(int_val));
217
218         switch (actionid) {
219         case IOV_GVAL(IOV_VERSION):
220                 /* Need to have checked buffer length */
221                 strncpy((char *)arg, dhd_version, len);
222                 break;
223
224         case IOV_GVAL(IOV_MSGLEVEL):
225                 int_val = (s32) dhd_msg_level;
226                 bcopy(&int_val, arg, val_size);
227                 break;
228
229         case IOV_SVAL(IOV_MSGLEVEL):
230                 dhd_msg_level = int_val;
231                 break;
232
233         case IOV_GVAL(IOV_BCMERRORSTR):
234                 strncpy((char *)arg, bcmerrorstr(dhd_pub->bcmerror),
235                         BCME_STRLEN);
236                 ((char *)arg)[BCME_STRLEN - 1] = 0x00;
237                 break;
238
239         case IOV_GVAL(IOV_BCMERROR):
240                 int_val = (s32) dhd_pub->bcmerror;
241                 bcopy(&int_val, arg, val_size);
242                 break;
243
244         case IOV_GVAL(IOV_WDTICK):
245                 int_val = (s32) dhd_watchdog_ms;
246                 bcopy(&int_val, arg, val_size);
247                 break;
248
249         case IOV_SVAL(IOV_WDTICK):
250                 if (!dhd_pub->up) {
251                         bcmerror = BCME_NOTUP;
252                         break;
253                 }
254                 dhd_os_wd_timer(dhd_pub, (uint) int_val);
255                 break;
256
257         case IOV_GVAL(IOV_DUMP):
258                 bcmerror = dhd_dump(dhd_pub, arg, len);
259                 break;
260
261 #ifdef DHD_DEBUG
262         case IOV_GVAL(IOV_DCONSOLE_POLL):
263                 int_val = (s32) dhd_console_ms;
264                 bcopy(&int_val, arg, val_size);
265                 break;
266
267         case IOV_SVAL(IOV_DCONSOLE_POLL):
268                 dhd_console_ms = (uint) int_val;
269                 break;
270
271         case IOV_SVAL(IOV_CONS):
272                 if (len > 0)
273                         bcmerror = dhd_bus_console_in(dhd_pub, arg, len - 1);
274                 break;
275 #endif
276
277         case IOV_SVAL(IOV_CLEARCOUNTS):
278                 dhd_pub->tx_packets = dhd_pub->rx_packets = 0;
279                 dhd_pub->tx_errors = dhd_pub->rx_errors = 0;
280                 dhd_pub->tx_ctlpkts = dhd_pub->rx_ctlpkts = 0;
281                 dhd_pub->tx_ctlerrs = dhd_pub->rx_ctlerrs = 0;
282                 dhd_pub->rx_dropped = 0;
283                 dhd_pub->rx_readahead_cnt = 0;
284                 dhd_pub->tx_realloc = 0;
285                 dhd_pub->wd_dpc_sched = 0;
286                 memset(&dhd_pub->dstats, 0, sizeof(dhd_pub->dstats));
287                 dhd_bus_clearcounts(dhd_pub);
288                 break;
289
290         case IOV_GVAL(IOV_IOCTLTIMEOUT):{
291                         int_val = (s32) dhd_os_get_ioctl_resp_timeout();
292                         bcopy(&int_val, arg, sizeof(int_val));
293                         break;
294                 }
295
296         case IOV_SVAL(IOV_IOCTLTIMEOUT):{
297                         if (int_val <= 0)
298                                 bcmerror = BCME_BADARG;
299                         else
300                                 dhd_os_set_ioctl_resp_timeout((unsigned int)
301                                                               int_val);
302                         break;
303                 }
304
305         default:
306                 bcmerror = BCME_UNSUPPORTED;
307                 break;
308         }
309
310 exit:
311         return bcmerror;
312 }
313
314 /* Store the status of a connection attempt for later retrieval by an iovar */
315 void dhd_store_conn_status(u32 event, u32 status, u32 reason)
316 {
317         /* Do not overwrite a WLC_E_PRUNE with a WLC_E_SET_SSID
318          * because an encryption/rsn mismatch results in both events, and
319          * the important information is in the WLC_E_PRUNE.
320          */
321         if (!(event == WLC_E_SET_SSID && status == WLC_E_STATUS_FAIL &&
322               dhd_conn_event == WLC_E_PRUNE)) {
323                 dhd_conn_event = event;
324                 dhd_conn_status = status;
325                 dhd_conn_reason = reason;
326         }
327 }
328
329 bool dhd_prec_enq(dhd_pub_t *dhdp, struct pktq *q, void *pkt, int prec)
330 {
331         void *p;
332         int eprec = -1;         /* precedence to evict from */
333         bool discard_oldest;
334
335         /* Fast case, precedence queue is not full and we are also not
336          * exceeding total queue length
337          */
338         if (!pktq_pfull(q, prec) && !pktq_full(q)) {
339                 pktq_penq(q, prec, pkt);
340                 return true;
341         }
342
343         /* Determine precedence from which to evict packet, if any */
344         if (pktq_pfull(q, prec))
345                 eprec = prec;
346         else if (pktq_full(q)) {
347                 p = pktq_peek_tail(q, &eprec);
348                 ASSERT(p);
349                 if (eprec > prec)
350                         return false;
351         }
352
353         /* Evict if needed */
354         if (eprec >= 0) {
355                 /* Detect queueing to unconfigured precedence */
356                 ASSERT(!pktq_pempty(q, eprec));
357                 discard_oldest = AC_BITMAP_TST(dhdp->wme_dp, eprec);
358                 if (eprec == prec && !discard_oldest)
359                         return false;   /* refuse newer (incoming) packet */
360                 /* Evict packet according to discard policy */
361                 p = discard_oldest ? pktq_pdeq(q, eprec) : pktq_pdeq_tail(q,
362                                                   eprec);
363                 if (p == NULL) {
364                         DHD_ERROR(("%s: pktq_penq() failed, oldest %d.",
365                                    __func__, discard_oldest));
366                         ASSERT(p);
367                 }
368
369                 PKTFREE(dhdp->osh, p, true);
370         }
371
372         /* Enqueue */
373         p = pktq_penq(q, prec, pkt);
374         if (p == NULL) {
375                 DHD_ERROR(("%s: pktq_penq() failed.", __func__));
376                 ASSERT(p);
377         }
378
379         return true;
380 }
381
382 static int
383 dhd_iovar_op(dhd_pub_t *dhd_pub, const char *name,
384              void *params, int plen, void *arg, int len, bool set)
385 {
386         int bcmerror = 0;
387         int val_size;
388         const bcm_iovar_t *vi = NULL;
389         u32 actionid;
390
391         DHD_TRACE(("%s: Enter\n", __func__));
392
393         ASSERT(name);
394         ASSERT(len >= 0);
395
396         /* Get MUST have return space */
397         ASSERT(set || (arg && len));
398
399         /* Set does NOT take qualifiers */
400         ASSERT(!set || (!params && !plen));
401
402         vi = bcm_iovar_lookup(dhd_iovars, name);
403         if (vi == NULL) {
404                 bcmerror = BCME_UNSUPPORTED;
405                 goto exit;
406         }
407
408         DHD_CTL(("%s: %s %s, len %d plen %d\n", __func__,
409                  name, (set ? "set" : "get"), len, plen));
410
411         /* set up 'params' pointer in case this is a set command so that
412          * the convenience int and bool code can be common to set and get
413          */
414         if (params == NULL) {
415                 params = arg;
416                 plen = len;
417         }
418
419         if (vi->type == IOVT_VOID)
420                 val_size = 0;
421         else if (vi->type == IOVT_BUFFER)
422                 val_size = len;
423         else
424                 /* all other types are integer sized */
425                 val_size = sizeof(int);
426
427         actionid = set ? IOV_SVAL(vi->varid) : IOV_GVAL(vi->varid);
428         bcmerror =
429             dhd_doiovar(dhd_pub, vi, actionid, name, params, plen, arg, len,
430                         val_size);
431
432 exit:
433         return bcmerror;
434 }
435
436 int dhd_ioctl(dhd_pub_t *dhd_pub, dhd_ioctl_t *ioc, void *buf, uint buflen)
437 {
438         int bcmerror = 0;
439
440         DHD_TRACE(("%s: Enter\n", __func__));
441
442         if (!buf)
443                 return BCME_BADARG;
444
445         switch (ioc->cmd) {
446         case DHD_GET_MAGIC:
447                 if (buflen < sizeof(int))
448                         bcmerror = BCME_BUFTOOSHORT;
449                 else
450                         *(int *)buf = DHD_IOCTL_MAGIC;
451                 break;
452
453         case DHD_GET_VERSION:
454                 if (buflen < sizeof(int))
455                         bcmerror = -BCME_BUFTOOSHORT;
456                 else
457                         *(int *)buf = DHD_IOCTL_VERSION;
458                 break;
459
460         case DHD_GET_VAR:
461         case DHD_SET_VAR:{
462                         char *arg;
463                         uint arglen;
464
465                         /* scan past the name to any arguments */
466                         for (arg = buf, arglen = buflen; *arg && arglen;
467                              arg++, arglen--)
468                                 ;
469
470                         if (*arg) {
471                                 bcmerror = BCME_BUFTOOSHORT;
472                                 break;
473                         }
474
475                         /* account for the NUL terminator */
476                         arg++, arglen--;
477
478                         /* call with the appropriate arguments */
479                         if (ioc->cmd == DHD_GET_VAR)
480                                 bcmerror =
481                                     dhd_iovar_op(dhd_pub, buf, arg, arglen, buf,
482                                                  buflen, IOV_GET);
483                         else
484                                 bcmerror =
485                                     dhd_iovar_op(dhd_pub, buf, NULL, 0, arg,
486                                                  arglen, IOV_SET);
487                         if (bcmerror != BCME_UNSUPPORTED)
488                                 break;
489
490                         /* not in generic table, try protocol module */
491                         if (ioc->cmd == DHD_GET_VAR)
492                                 bcmerror = dhd_prot_iovar_op(dhd_pub, buf, arg,
493                                                              arglen, buf,
494                                                              buflen, IOV_GET);
495                         else
496                                 bcmerror = dhd_prot_iovar_op(dhd_pub, buf,
497                                                              NULL, 0, arg,
498                                                              arglen, IOV_SET);
499                         if (bcmerror != BCME_UNSUPPORTED)
500                                 break;
501
502                         /* if still not found, try bus module */
503                         if (ioc->cmd == DHD_GET_VAR)
504                                 bcmerror = dhd_bus_iovar_op(dhd_pub, buf,
505                                                             arg, arglen, buf,
506                                                             buflen, IOV_GET);
507                         else
508                                 bcmerror = dhd_bus_iovar_op(dhd_pub, buf,
509                                                             NULL, 0, arg,
510                                                             arglen, IOV_SET);
511
512                         break;
513                 }
514
515         default:
516                 bcmerror = BCME_UNSUPPORTED;
517         }
518
519         return bcmerror;
520 }
521
522 #ifdef SHOW_EVENTS
523 static void wl_show_host_event(wl_event_msg_t *event, void *event_data)
524 {
525         uint i, status, reason;
526         bool group = false, flush_txq = false, link = false;
527         char *auth_str, *event_name;
528         unsigned char *buf;
529         char err_msg[256], eabuf[ETHER_ADDR_STR_LEN];
530         static struct {
531                 uint event;
532                 char *event_name;
533         } event_names[] = {
534                 {
535                 WLC_E_SET_SSID, "SET_SSID"}, {
536                 WLC_E_JOIN, "JOIN"}, {
537                 WLC_E_START, "START"}, {
538                 WLC_E_AUTH, "AUTH"}, {
539                 WLC_E_AUTH_IND, "AUTH_IND"}, {
540                 WLC_E_DEAUTH, "DEAUTH"}, {
541                 WLC_E_DEAUTH_IND, "DEAUTH_IND"}, {
542                 WLC_E_ASSOC, "ASSOC"}, {
543                 WLC_E_ASSOC_IND, "ASSOC_IND"}, {
544                 WLC_E_REASSOC, "REASSOC"}, {
545                 WLC_E_REASSOC_IND, "REASSOC_IND"}, {
546                 WLC_E_DISASSOC, "DISASSOC"}, {
547                 WLC_E_DISASSOC_IND, "DISASSOC_IND"}, {
548                 WLC_E_QUIET_START, "START_QUIET"}, {
549                 WLC_E_QUIET_END, "END_QUIET"}, {
550                 WLC_E_BEACON_RX, "BEACON_RX"}, {
551                 WLC_E_LINK, "LINK"}, {
552                 WLC_E_MIC_ERROR, "MIC_ERROR"}, {
553                 WLC_E_NDIS_LINK, "NDIS_LINK"}, {
554                 WLC_E_ROAM, "ROAM"}, {
555                 WLC_E_TXFAIL, "TXFAIL"}, {
556                 WLC_E_PMKID_CACHE, "PMKID_CACHE"}, {
557                 WLC_E_RETROGRADE_TSF, "RETROGRADE_TSF"}, {
558                 WLC_E_PRUNE, "PRUNE"}, {
559                 WLC_E_AUTOAUTH, "AUTOAUTH"}, {
560                 WLC_E_EAPOL_MSG, "EAPOL_MSG"}, {
561                 WLC_E_SCAN_COMPLETE, "SCAN_COMPLETE"}, {
562                 WLC_E_ADDTS_IND, "ADDTS_IND"}, {
563                 WLC_E_DELTS_IND, "DELTS_IND"}, {
564                 WLC_E_BCNSENT_IND, "BCNSENT_IND"}, {
565                 WLC_E_BCNRX_MSG, "BCNRX_MSG"}, {
566                 WLC_E_BCNLOST_MSG, "BCNLOST_MSG"}, {
567                 WLC_E_ROAM_PREP, "ROAM_PREP"}, {
568                 WLC_E_PFN_NET_FOUND, "PNO_NET_FOUND"}, {
569                 WLC_E_PFN_NET_LOST, "PNO_NET_LOST"}, {
570                 WLC_E_RESET_COMPLETE, "RESET_COMPLETE"}, {
571                 WLC_E_JOIN_START, "JOIN_START"}, {
572                 WLC_E_ROAM_START, "ROAM_START"}, {
573                 WLC_E_ASSOC_START, "ASSOC_START"}, {
574                 WLC_E_IBSS_ASSOC, "IBSS_ASSOC"}, {
575                 WLC_E_RADIO, "RADIO"}, {
576                 WLC_E_PSM_WATCHDOG, "PSM_WATCHDOG"}, {
577                 WLC_E_PROBREQ_MSG, "PROBREQ_MSG"}, {
578                 WLC_E_SCAN_CONFIRM_IND, "SCAN_CONFIRM_IND"}, {
579                 WLC_E_PSK_SUP, "PSK_SUP"}, {
580                 WLC_E_COUNTRY_CODE_CHANGED, "COUNTRY_CODE_CHANGED"}, {
581                 WLC_E_EXCEEDED_MEDIUM_TIME, "EXCEEDED_MEDIUM_TIME"}, {
582                 WLC_E_ICV_ERROR, "ICV_ERROR"}, {
583                 WLC_E_UNICAST_DECODE_ERROR, "UNICAST_DECODE_ERROR"}, {
584                 WLC_E_MULTICAST_DECODE_ERROR, "MULTICAST_DECODE_ERROR"}, {
585                 WLC_E_TRACE, "TRACE"}, {
586                 WLC_E_ACTION_FRAME, "ACTION FRAME"}, {
587                 WLC_E_ACTION_FRAME_COMPLETE, "ACTION FRAME TX COMPLETE"}, {
588                 WLC_E_IF, "IF"}, {
589                 WLC_E_RSSI, "RSSI"}, {
590                 WLC_E_PFN_SCAN_COMPLETE, "SCAN_COMPLETE"}
591         };
592         uint event_type, flags, auth_type, datalen;
593         event_type = ntoh32(event->event_type);
594         flags = ntoh16(event->flags);
595         status = ntoh32(event->status);
596         reason = ntoh32(event->reason);
597         auth_type = ntoh32(event->auth_type);
598         datalen = ntoh32(event->datalen);
599         /* debug dump of event messages */
600         sprintf(eabuf, "%pM", event->addr.octet);
601
602         event_name = "UNKNOWN";
603         for (i = 0; i < ARRAY_SIZE(event_names); i++) {
604                 if (event_names[i].event == event_type)
605                         event_name = event_names[i].event_name;
606         }
607
608         DHD_EVENT(("EVENT: %s, event ID = %d\n", event_name, event_type));
609
610         if (flags & WLC_EVENT_MSG_LINK)
611                 link = true;
612         if (flags & WLC_EVENT_MSG_GROUP)
613                 group = true;
614         if (flags & WLC_EVENT_MSG_FLUSHTXQ)
615                 flush_txq = true;
616
617         switch (event_type) {
618         case WLC_E_START:
619         case WLC_E_DEAUTH:
620         case WLC_E_DISASSOC:
621                 DHD_EVENT(("MACEVENT: %s, MAC %s\n", event_name, eabuf));
622                 break;
623
624         case WLC_E_ASSOC_IND:
625         case WLC_E_REASSOC_IND:
626                 DHD_EVENT(("MACEVENT: %s, MAC %s\n", event_name, eabuf));
627                 break;
628
629         case WLC_E_ASSOC:
630         case WLC_E_REASSOC:
631                 if (status == WLC_E_STATUS_SUCCESS) {
632                         DHD_EVENT(("MACEVENT: %s, MAC %s, SUCCESS\n",
633                                    event_name, eabuf));
634                 } else if (status == WLC_E_STATUS_TIMEOUT) {
635                         DHD_EVENT(("MACEVENT: %s, MAC %s, TIMEOUT\n",
636                                    event_name, eabuf));
637                 } else if (status == WLC_E_STATUS_FAIL) {
638                         DHD_EVENT(("MACEVENT: %s, MAC %s, FAILURE, reason %d\n",
639                                    event_name, eabuf, (int)reason));
640                 } else {
641                         DHD_EVENT(("MACEVENT: %s, MAC %s, unexpected status "
642                                 "%d\n", event_name, eabuf, (int)status));
643                 }
644                 break;
645
646         case WLC_E_DEAUTH_IND:
647         case WLC_E_DISASSOC_IND:
648                 DHD_EVENT(("MACEVENT: %s, MAC %s, reason %d\n", event_name,
649                            eabuf, (int)reason));
650                 break;
651
652         case WLC_E_AUTH:
653         case WLC_E_AUTH_IND:
654                 if (auth_type == DOT11_OPEN_SYSTEM)
655                         auth_str = "Open System";
656                 else if (auth_type == DOT11_SHARED_KEY)
657                         auth_str = "Shared Key";
658                 else {
659                         sprintf(err_msg, "AUTH unknown: %d", (int)auth_type);
660                         auth_str = err_msg;
661                 }
662                 if (event_type == WLC_E_AUTH_IND) {
663                         DHD_EVENT(("MACEVENT: %s, MAC %s, %s\n", event_name,
664                                    eabuf, auth_str));
665                 } else if (status == WLC_E_STATUS_SUCCESS) {
666                         DHD_EVENT(("MACEVENT: %s, MAC %s, %s, SUCCESS\n",
667                                    event_name, eabuf, auth_str));
668                 } else if (status == WLC_E_STATUS_TIMEOUT) {
669                         DHD_EVENT(("MACEVENT: %s, MAC %s, %s, TIMEOUT\n",
670                                    event_name, eabuf, auth_str));
671                 } else if (status == WLC_E_STATUS_FAIL) {
672                         DHD_EVENT(("MACEVENT: %s, MAC %s, %s, FAILURE, "
673                                 "reason %d\n",
674                                 event_name, eabuf, auth_str, (int)reason));
675                 }
676
677                 break;
678
679         case WLC_E_JOIN:
680         case WLC_E_ROAM:
681         case WLC_E_SET_SSID:
682                 if (status == WLC_E_STATUS_SUCCESS) {
683                         DHD_EVENT(("MACEVENT: %s, MAC %s\n", event_name,
684                                    eabuf));
685                 } else if (status == WLC_E_STATUS_FAIL) {
686                         DHD_EVENT(("MACEVENT: %s, failed\n", event_name));
687                 } else if (status == WLC_E_STATUS_NO_NETWORKS) {
688                         DHD_EVENT(("MACEVENT: %s, no networks found\n",
689                                    event_name));
690                 } else {
691                         DHD_EVENT(("MACEVENT: %s, unexpected status %d\n",
692                                    event_name, (int)status));
693                 }
694                 break;
695
696         case WLC_E_BEACON_RX:
697                 if (status == WLC_E_STATUS_SUCCESS) {
698                         DHD_EVENT(("MACEVENT: %s, SUCCESS\n", event_name));
699                 } else if (status == WLC_E_STATUS_FAIL) {
700                         DHD_EVENT(("MACEVENT: %s, FAIL\n", event_name));
701                 } else {
702                         DHD_EVENT(("MACEVENT: %s, status %d\n", event_name,
703                                    status));
704                 }
705                 break;
706
707         case WLC_E_LINK:
708                 DHD_EVENT(("MACEVENT: %s %s\n", event_name,
709                            link ? "UP" : "DOWN"));
710                 break;
711
712         case WLC_E_MIC_ERROR:
713                 DHD_EVENT(("MACEVENT: %s, MAC %s, Group %d, Flush %d\n",
714                            event_name, eabuf, group, flush_txq));
715                 break;
716
717         case WLC_E_ICV_ERROR:
718         case WLC_E_UNICAST_DECODE_ERROR:
719         case WLC_E_MULTICAST_DECODE_ERROR:
720                 DHD_EVENT(("MACEVENT: %s, MAC %s\n", event_name, eabuf));
721                 break;
722
723         case WLC_E_TXFAIL:
724                 DHD_EVENT(("MACEVENT: %s, RA %s\n", event_name, eabuf));
725                 break;
726
727         case WLC_E_SCAN_COMPLETE:
728         case WLC_E_PMKID_CACHE:
729                 DHD_EVENT(("MACEVENT: %s\n", event_name));
730                 break;
731
732         case WLC_E_PFN_NET_FOUND:
733         case WLC_E_PFN_NET_LOST:
734         case WLC_E_PFN_SCAN_COMPLETE:
735                 DHD_EVENT(("PNOEVENT: %s\n", event_name));
736                 break;
737
738         case WLC_E_PSK_SUP:
739         case WLC_E_PRUNE:
740                 DHD_EVENT(("MACEVENT: %s, status %d, reason %d\n",
741                            event_name, (int)status, (int)reason));
742                 break;
743
744         case WLC_E_TRACE:
745                 {
746                         static u32 seqnum_prev;
747                         msgtrace_hdr_t hdr;
748                         u32 nblost;
749                         char *s, *p;
750
751                         buf = (unsigned char *) event_data;
752                         memcpy(&hdr, buf, MSGTRACE_HDRLEN);
753
754                         if (hdr.version != MSGTRACE_VERSION) {
755                                 printf
756                                     ("\nMACEVENT: %s [unsupported version --> "
757                                      "dhd version:%d dongle version:%d]\n",
758                                      event_name, MSGTRACE_VERSION, hdr.version);
759                                 /* Reset datalen to avoid display below */
760                                 datalen = 0;
761                                 break;
762                         }
763
764                         /* There are 2 bytes available at the end of data */
765                         buf[MSGTRACE_HDRLEN + ntoh16(hdr.len)] = '\0';
766
767                         if (ntoh32(hdr.discarded_bytes)
768                             || ntoh32(hdr.discarded_printf)) {
769                                 printf
770                                     ("\nWLC_E_TRACE: [Discarded traces in dongle -->"
771                                      "discarded_bytes %d discarded_printf %d]\n",
772                                      ntoh32(hdr.discarded_bytes),
773                                      ntoh32(hdr.discarded_printf));
774                         }
775
776                         nblost = ntoh32(hdr.seqnum) - seqnum_prev - 1;
777                         if (nblost > 0) {
778                                 printf
779                                     ("\nWLC_E_TRACE: [Event lost --> seqnum %d nblost %d\n",
780                                      ntoh32(hdr.seqnum), nblost);
781                         }
782                         seqnum_prev = ntoh32(hdr.seqnum);
783
784                         /* Display the trace buffer. Advance from \n to \n to
785                          * avoid display big
786                          * printf (issue with Linux printk )
787                          */
788                         p = (char *)&buf[MSGTRACE_HDRLEN];
789                         while ((s = strstr(p, "\n")) != NULL) {
790                                 *s = '\0';
791                                 printf("%s\n", p);
792                                 p = s + 1;
793                         }
794                         printf("%s\n", p);
795
796                         /* Reset datalen to avoid display below */
797                         datalen = 0;
798                 }
799                 break;
800
801         case WLC_E_RSSI:
802                 DHD_EVENT(("MACEVENT: %s %d\n", event_name,
803                            ntoh32(*((int *)event_data))));
804                 break;
805
806         default:
807                 DHD_EVENT(("MACEVENT: %s %d, MAC %s, status %d, reason %d, "
808                         "auth %d\n", event_name, event_type, eabuf,
809                         (int)status, (int)reason, (int)auth_type));
810                 break;
811         }
812
813         /* show any appended data */
814         if (datalen) {
815                 buf = (unsigned char *) event_data;
816                 DHD_EVENT((" data (%d) : ", datalen));
817                 for (i = 0; i < datalen; i++)
818                         DHD_EVENT((" 0x%02x ", *buf++));
819                 DHD_EVENT(("\n"));
820         }
821 }
822 #endif                          /* SHOW_EVENTS */
823
824 int
825 wl_host_event(struct dhd_info *dhd, int *ifidx, void *pktdata,
826               wl_event_msg_t *event, void **data_ptr)
827 {
828         /* check whether packet is a BRCM event pkt */
829         bcm_event_t *pvt_data = (bcm_event_t *) pktdata;
830         char *event_data;
831         u32 type, status;
832         u16 flags;
833         int evlen;
834
835         if (bcmp(BRCM_OUI, &pvt_data->bcm_hdr.oui[0], DOT11_OUI_LEN)) {
836                 DHD_ERROR(("%s: mismatched OUI, bailing\n", __func__));
837                 return BCME_ERROR;
838         }
839
840         /* BRCM event pkt may be unaligned - use xxx_ua to load user_subtype. */
841         if (ntoh16_ua((void *)&pvt_data->bcm_hdr.usr_subtype) !=
842             BCMILCP_BCM_SUBTYPE_EVENT) {
843                 DHD_ERROR(("%s: mismatched subtype, bailing\n", __func__));
844                 return BCME_ERROR;
845         }
846
847         *data_ptr = &pvt_data[1];
848         event_data = *data_ptr;
849
850         /* memcpy since BRCM event pkt may be unaligned. */
851         memcpy(event, &pvt_data->event, sizeof(wl_event_msg_t));
852
853         type = ntoh32_ua((void *)&event->event_type);
854         flags = ntoh16_ua((void *)&event->flags);
855         status = ntoh32_ua((void *)&event->status);
856         evlen = ntoh32_ua((void *)&event->datalen) + sizeof(bcm_event_t);
857
858         switch (type) {
859         case WLC_E_IF:
860                 {
861                         dhd_if_event_t *ifevent = (dhd_if_event_t *) event_data;
862                         DHD_TRACE(("%s: if event\n", __func__));
863
864                         if (ifevent->ifidx > 0 &&
865                                  ifevent->ifidx < DHD_MAX_IFS) {
866                                 if (ifevent->action == WLC_E_IF_ADD)
867                                         dhd_add_if(dhd, ifevent->ifidx,
868                                                    NULL, event->ifname,
869                                                    pvt_data->eth.ether_dhost,
870                                                    ifevent->flags,
871                                                    ifevent->bssidx);
872                                 else
873                                         dhd_del_if(dhd, ifevent->ifidx);
874                         } else {
875                                 DHD_ERROR(("%s: Invalid ifidx %d for %s\n",
876                                            __func__, ifevent->ifidx,
877                                            event->ifname));
878                         }
879                 }
880                 /* send up the if event: btamp user needs it */
881                 *ifidx = dhd_ifname2idx(dhd, event->ifname);
882                 /* push up to external supp/auth */
883                 dhd_event(dhd, (char *)pvt_data, evlen, *ifidx);
884                 break;
885
886 #ifdef P2P
887         case WLC_E_NDIS_LINK:
888                 break;
889 #endif
890                 /* fall through */
891                 /* These are what external supplicant/authenticator wants */
892         case WLC_E_LINK:
893         case WLC_E_ASSOC_IND:
894         case WLC_E_REASSOC_IND:
895         case WLC_E_DISASSOC_IND:
896         case WLC_E_MIC_ERROR:
897         default:
898                 /* Fall through: this should get _everything_  */
899
900                 *ifidx = dhd_ifname2idx(dhd, event->ifname);
901                 /* push up to external supp/auth */
902                 dhd_event(dhd, (char *)pvt_data, evlen, *ifidx);
903                 DHD_TRACE(("%s: MAC event %d, flags %x, status %x\n",
904                            __func__, type, flags, status));
905
906                 /* put it back to WLC_E_NDIS_LINK */
907                 if (type == WLC_E_NDIS_LINK) {
908                         u32 temp;
909
910                         temp = ntoh32_ua((void *)&event->event_type);
911                         DHD_TRACE(("Converted to WLC_E_LINK type %d\n", temp));
912
913                         temp = ntoh32(WLC_E_NDIS_LINK);
914                         memcpy((void *)(&pvt_data->event.event_type), &temp,
915                                sizeof(pvt_data->event.event_type));
916                 }
917                 break;
918         }
919
920 #ifdef SHOW_EVENTS
921         wl_show_host_event(event, event_data);
922 #endif                          /* SHOW_EVENTS */
923
924         return BCME_OK;
925 }
926
927 void wl_event_to_host_order(wl_event_msg_t *evt)
928 {
929         /* Event struct members passed from dongle to host are stored
930          * in network
931          * byte order. Convert all members to host-order.
932          */
933         evt->event_type = ntoh32(evt->event_type);
934         evt->flags = ntoh16(evt->flags);
935         evt->status = ntoh32(evt->status);
936         evt->reason = ntoh32(evt->reason);
937         evt->auth_type = ntoh32(evt->auth_type);
938         evt->datalen = ntoh32(evt->datalen);
939         evt->version = ntoh16(evt->version);
940 }
941
942 void print_buf(void *pbuf, int len, int bytes_per_line)
943 {
944         int i, j = 0;
945         unsigned char *buf = pbuf;
946
947         if (bytes_per_line == 0)
948                 bytes_per_line = len;
949
950         for (i = 0; i < len; i++) {
951                 printf("%2.2x", *buf++);
952                 j++;
953                 if (j == bytes_per_line) {
954                         printf("\n");
955                         j = 0;
956                 } else {
957                         printf(":");
958                 }
959         }
960         printf("\n");
961 }
962
963 /* Convert user's input in hex pattern to byte-size mask */
964 static int wl_pattern_atoh(char *src, char *dst)
965 {
966         int i;
967         if (strncmp(src, "0x", 2) != 0 && strncmp(src, "0X", 2) != 0) {
968                 DHD_ERROR(("Mask invalid format. Needs to start with 0x\n"));
969                 return -1;
970         }
971         src = src + 2;          /* Skip past 0x */
972         if (strlen(src) % 2 != 0) {
973                 DHD_ERROR(("Mask invalid format. Length must be even.\n"));
974                 return -1;
975         }
976         for (i = 0; *src != '\0'; i++) {
977                 char num[3];
978                 strncpy(num, src, 2);
979                 num[2] = '\0';
980                 dst[i] = (u8) simple_strtoul(num, NULL, 16);
981                 src += 2;
982         }
983         return i;
984 }
985
986 void
987 dhd_pktfilter_offload_enable(dhd_pub_t *dhd, char *arg, int enable,
988                              int master_mode)
989 {
990         char *argv[8];
991         int i = 0;
992         const char *str;
993         int buf_len;
994         int str_len;
995         char *arg_save = 0, *arg_org = 0;
996         int rc;
997         char buf[128];
998         wl_pkt_filter_enable_t enable_parm;
999         wl_pkt_filter_enable_t *pkt_filterp;
1000
1001         arg_save = kmalloc(strlen(arg) + 1, GFP_ATOMIC);
1002         if (!arg_save) {
1003                 DHD_ERROR(("%s: kmalloc failed\n", __func__));
1004                 goto fail;
1005         }
1006         arg_org = arg_save;
1007         memcpy(arg_save, arg, strlen(arg) + 1);
1008
1009         argv[i] = strsep(&arg_save, " ");
1010
1011         i = 0;
1012         if (NULL == argv[i]) {
1013                 DHD_ERROR(("No args provided\n"));
1014                 goto fail;
1015         }
1016
1017         str = "pkt_filter_enable";
1018         str_len = strlen(str);
1019         strncpy(buf, str, str_len);
1020         buf[str_len] = '\0';
1021         buf_len = str_len + 1;
1022
1023         pkt_filterp = (wl_pkt_filter_enable_t *) (buf + str_len + 1);
1024
1025         /* Parse packet filter id. */
1026         enable_parm.id = htod32(simple_strtoul(argv[i], NULL, 0));
1027
1028         /* Parse enable/disable value. */
1029         enable_parm.enable = htod32(enable);
1030
1031         buf_len += sizeof(enable_parm);
1032         memcpy((char *)pkt_filterp, &enable_parm, sizeof(enable_parm));
1033
1034         /* Enable/disable the specified filter. */
1035         rc = dhdcdc_set_ioctl(dhd, 0, WLC_SET_VAR, buf, buf_len);
1036         rc = rc >= 0 ? 0 : rc;
1037         if (rc)
1038                 DHD_TRACE(("%s: failed to add pktfilter %s, retcode = %d\n",
1039                            __func__, arg, rc));
1040         else
1041                 DHD_TRACE(("%s: successfully added pktfilter %s\n",
1042                            __func__, arg));
1043
1044         /* Contorl the master mode */
1045         bcm_mkiovar("pkt_filter_mode", (char *)&master_mode, 4, buf,
1046                     sizeof(buf));
1047         rc = dhdcdc_set_ioctl(dhd, 0, WLC_SET_VAR, buf, sizeof(buf));
1048         rc = rc >= 0 ? 0 : rc;
1049         if (rc)
1050                 DHD_TRACE(("%s: failed to add pktfilter %s, retcode = %d\n",
1051                            __func__, arg, rc));
1052
1053 fail:
1054         if (arg_org)
1055                 kfree(arg_org);
1056 }
1057
1058 void dhd_pktfilter_offload_set(dhd_pub_t *dhd, char *arg)
1059 {
1060         const char *str;
1061         wl_pkt_filter_t pkt_filter;
1062         wl_pkt_filter_t *pkt_filterp;
1063         int buf_len;
1064         int str_len;
1065         int rc;
1066         u32 mask_size;
1067         u32 pattern_size;
1068         char *argv[8], *buf = 0;
1069         int i = 0;
1070         char *arg_save = 0, *arg_org = 0;
1071 #define BUF_SIZE                2048
1072
1073         arg_save = kmalloc(strlen(arg) + 1, GFP_ATOMIC);
1074         if (!arg_save) {
1075                 DHD_ERROR(("%s: kmalloc failed\n", __func__));
1076                 goto fail;
1077         }
1078
1079         arg_org = arg_save;
1080
1081         buf = kmalloc(BUF_SIZE, GFP_ATOMIC);
1082         if (!buf) {
1083                 DHD_ERROR(("%s: kmalloc failed\n", __func__));
1084                 goto fail;
1085         }
1086
1087         memcpy(arg_save, arg, strlen(arg) + 1);
1088
1089         if (strlen(arg) > BUF_SIZE) {
1090                 DHD_ERROR(("Not enough buffer %d < %d\n", (int)strlen(arg),
1091                            (int)sizeof(buf)));
1092                 goto fail;
1093         }
1094
1095         argv[i] = strsep(&arg_save, " ");
1096         while (argv[i++])
1097                 argv[i] = strsep(&arg_save, " ");
1098
1099         i = 0;
1100         if (NULL == argv[i]) {
1101                 DHD_ERROR(("No args provided\n"));
1102                 goto fail;
1103         }
1104
1105         str = "pkt_filter_add";
1106         str_len = strlen(str);
1107         strncpy(buf, str, str_len);
1108         buf[str_len] = '\0';
1109         buf_len = str_len + 1;
1110
1111         pkt_filterp = (wl_pkt_filter_t *) (buf + str_len + 1);
1112
1113         /* Parse packet filter id. */
1114         pkt_filter.id = htod32(simple_strtoul(argv[i], NULL, 0));
1115
1116         if (NULL == argv[++i]) {
1117                 DHD_ERROR(("Polarity not provided\n"));
1118                 goto fail;
1119         }
1120
1121         /* Parse filter polarity. */
1122         pkt_filter.negate_match = htod32(simple_strtoul(argv[i], NULL, 0));
1123
1124         if (NULL == argv[++i]) {
1125                 DHD_ERROR(("Filter type not provided\n"));
1126                 goto fail;
1127         }
1128
1129         /* Parse filter type. */
1130         pkt_filter.type = htod32(simple_strtoul(argv[i], NULL, 0));
1131
1132         if (NULL == argv[++i]) {
1133                 DHD_ERROR(("Offset not provided\n"));
1134                 goto fail;
1135         }
1136
1137         /* Parse pattern filter offset. */
1138         pkt_filter.u.pattern.offset = htod32(simple_strtoul(argv[i], NULL, 0));
1139
1140         if (NULL == argv[++i]) {
1141                 DHD_ERROR(("Bitmask not provided\n"));
1142                 goto fail;
1143         }
1144
1145         /* Parse pattern filter mask. */
1146         mask_size =
1147             htod32(wl_pattern_atoh
1148                    (argv[i], (char *)pkt_filterp->u.pattern.mask_and_pattern));
1149
1150         if (NULL == argv[++i]) {
1151                 DHD_ERROR(("Pattern not provided\n"));
1152                 goto fail;
1153         }
1154
1155         /* Parse pattern filter pattern. */
1156         pattern_size =
1157             htod32(wl_pattern_atoh(argv[i],
1158                                    (char *)&pkt_filterp->u.pattern.
1159                                    mask_and_pattern[mask_size]));
1160
1161         if (mask_size != pattern_size) {
1162                 DHD_ERROR(("Mask and pattern not the same size\n"));
1163                 goto fail;
1164         }
1165
1166         pkt_filter.u.pattern.size_bytes = mask_size;
1167         buf_len += WL_PKT_FILTER_FIXED_LEN;
1168         buf_len += (WL_PKT_FILTER_PATTERN_FIXED_LEN + 2 * mask_size);
1169
1170         /* Keep-alive attributes are set in local
1171          * variable (keep_alive_pkt), and
1172          ** then memcpy'ed into buffer (keep_alive_pktp) since there is no
1173          ** guarantee that the buffer is properly aligned.
1174          */
1175         memcpy((char *)pkt_filterp,
1176                &pkt_filter,
1177                WL_PKT_FILTER_FIXED_LEN + WL_PKT_FILTER_PATTERN_FIXED_LEN);
1178
1179         rc = dhdcdc_set_ioctl(dhd, 0, WLC_SET_VAR, buf, buf_len);
1180         rc = rc >= 0 ? 0 : rc;
1181
1182         if (rc)
1183                 DHD_TRACE(("%s: failed to add pktfilter %s, retcode = %d\n",
1184                            __func__, arg, rc));
1185         else
1186                 DHD_TRACE(("%s: successfully added pktfilter %s\n",
1187                            __func__, arg));
1188
1189 fail:
1190         if (arg_org)
1191                 kfree(arg_org);
1192
1193         if (buf)
1194                 kfree(buf);
1195 }
1196
1197 void dhd_arp_offload_set(dhd_pub_t *dhd, int arp_mode)
1198 {
1199         char iovbuf[32];
1200         int retcode;
1201
1202         bcm_mkiovar("arp_ol", (char *)&arp_mode, 4, iovbuf, sizeof(iovbuf));
1203         retcode = dhdcdc_set_ioctl(dhd, 0, WLC_SET_VAR, iovbuf, sizeof(iovbuf));
1204         retcode = retcode >= 0 ? 0 : retcode;
1205         if (retcode)
1206                 DHD_TRACE(("%s: failed to set ARP offload mode to 0x%x, "
1207                         "retcode = %d\n", __func__, arp_mode, retcode));
1208         else
1209                 DHD_TRACE(("%s: successfully set ARP offload mode to 0x%x\n",
1210                            __func__, arp_mode));
1211 }
1212
1213 void dhd_arp_offload_enable(dhd_pub_t *dhd, int arp_enable)
1214 {
1215         char iovbuf[32];
1216         int retcode;
1217
1218         bcm_mkiovar("arpoe", (char *)&arp_enable, 4, iovbuf, sizeof(iovbuf));
1219         retcode = dhdcdc_set_ioctl(dhd, 0, WLC_SET_VAR, iovbuf, sizeof(iovbuf));
1220         retcode = retcode >= 0 ? 0 : retcode;
1221         if (retcode)
1222                 DHD_TRACE(("%s: failed to enabe ARP offload to %d, "
1223                         "retcode = %d\n", __func__, arp_enable, retcode));
1224         else
1225                 DHD_TRACE(("%s: successfully enabed ARP offload to %d\n",
1226                            __func__, arp_enable));
1227 }
1228
1229 int dhd_preinit_ioctls(dhd_pub_t *dhd)
1230 {
1231         char iovbuf[WL_EVENTING_MASK_LEN + 12]; /*  Room for
1232                                  "event_msgs" + '\0' + bitvec  */
1233         uint up = 0;
1234         char buf[128], *ptr;
1235         uint power_mode = PM_FAST;
1236         u32 dongle_align = DHD_SDALIGN;
1237         u32 glom = 0;
1238         uint bcn_timeout = 3;
1239         int scan_assoc_time = 40;
1240         int scan_unassoc_time = 40;
1241 #ifdef GET_CUSTOM_MAC_ENABLE
1242         int ret = 0;
1243         struct ether_addr ea_addr;
1244 #endif                          /* GET_CUSTOM_MAC_ENABLE */
1245
1246         dhd_os_proto_block(dhd);
1247
1248 #ifdef GET_CUSTOM_MAC_ENABLE
1249         /* Read MAC address from external customer place
1250          ** NOTE that default mac address has to be present in
1251          ** otp or nvram file to bring up
1252          ** firmware but unique per board mac address maybe provided by
1253          ** customer code
1254          */
1255         ret = dhd_custom_get_mac_address(ea_addr.octet);
1256         if (!ret) {
1257                 bcm_mkiovar("cur_etheraddr", (void *)&ea_addr, ETHER_ADDR_LEN,
1258                             buf, sizeof(buf));
1259                 ret = dhdcdc_set_ioctl(dhd, 0, WLC_SET_VAR, buf, sizeof(buf));
1260                 if (ret < 0) {
1261                         DHD_ERROR(("%s: can't set MAC address , error=%d\n",
1262                                    __func__, ret));
1263                 } else
1264                         memcpy(dhd->mac.octet, (void *)&ea_addr,
1265                                ETHER_ADDR_LEN);
1266         }
1267 #endif                          /* GET_CUSTOM_MAC_ENABLE */
1268
1269         /* Set Country code */
1270         if (dhd->country_code[0] != 0) {
1271                 if (dhdcdc_set_ioctl(dhd, 0, WLC_SET_COUNTRY,
1272                                      dhd->country_code,
1273                                      sizeof(dhd->country_code)) < 0) {
1274                         DHD_ERROR(("%s: country code setting failed\n",
1275                                    __func__));
1276                 }
1277         }
1278
1279         /* query for 'ver' to get version info from firmware */
1280         memset(buf, 0, sizeof(buf));
1281         ptr = buf;
1282         bcm_mkiovar("ver", 0, 0, buf, sizeof(buf));
1283         dhdcdc_query_ioctl(dhd, 0, WLC_GET_VAR, buf, sizeof(buf));
1284         strsep(&ptr, "\n");
1285         /* Print fw version info */
1286         DHD_ERROR(("Firmware version = %s\n", buf));
1287
1288         /* Set PowerSave mode */
1289         dhdcdc_set_ioctl(dhd, 0, WLC_SET_PM, (char *)&power_mode,
1290                          sizeof(power_mode));
1291
1292         /* Match Host and Dongle rx alignment */
1293         bcm_mkiovar("bus:txglomalign", (char *)&dongle_align, 4, iovbuf,
1294                     sizeof(iovbuf));
1295         dhdcdc_set_ioctl(dhd, 0, WLC_SET_VAR, iovbuf, sizeof(iovbuf));
1296
1297         /* disable glom option per default */
1298         bcm_mkiovar("bus:txglom", (char *)&glom, 4, iovbuf, sizeof(iovbuf));
1299         dhdcdc_set_ioctl(dhd, 0, WLC_SET_VAR, iovbuf, sizeof(iovbuf));
1300
1301         /* Setup timeout if Beacons are lost and roam is off to report
1302                  link down */
1303         bcm_mkiovar("bcn_timeout", (char *)&bcn_timeout, 4, iovbuf,
1304                     sizeof(iovbuf));
1305         dhdcdc_set_ioctl(dhd, 0, WLC_SET_VAR, iovbuf, sizeof(iovbuf));
1306
1307         /* Enable/Disable build-in roaming to allowed ext supplicant to take
1308                  of romaing */
1309         bcm_mkiovar("roam_off", (char *)&dhd_roam, 4, iovbuf, sizeof(iovbuf));
1310         dhdcdc_set_ioctl(dhd, 0, WLC_SET_VAR, iovbuf, sizeof(iovbuf));
1311
1312         /* Force STA UP */
1313         if (dhd_radio_up)
1314                 dhdcdc_set_ioctl(dhd, 0, WLC_UP, (char *)&up, sizeof(up));
1315
1316         /* Setup event_msgs */
1317         bcm_mkiovar("event_msgs", dhd->eventmask, WL_EVENTING_MASK_LEN, iovbuf,
1318                     sizeof(iovbuf));
1319         dhdcdc_set_ioctl(dhd, 0, WLC_SET_VAR, iovbuf, sizeof(iovbuf));
1320
1321         dhdcdc_set_ioctl(dhd, 0, WLC_SET_SCAN_CHANNEL_TIME,
1322                          (char *)&scan_assoc_time, sizeof(scan_assoc_time));
1323         dhdcdc_set_ioctl(dhd, 0, WLC_SET_SCAN_UNASSOC_TIME,
1324                          (char *)&scan_unassoc_time, sizeof(scan_unassoc_time));
1325
1326 #ifdef ARP_OFFLOAD_SUPPORT
1327         /* Set and enable ARP offload feature */
1328         if (dhd_arp_enable)
1329                 dhd_arp_offload_set(dhd, dhd_arp_mode);
1330         dhd_arp_offload_enable(dhd, dhd_arp_enable);
1331 #endif                          /* ARP_OFFLOAD_SUPPORT */
1332
1333 #ifdef PKT_FILTER_SUPPORT
1334         {
1335                 int i;
1336                 /* Set up pkt filter */
1337                 if (dhd_pkt_filter_enable) {
1338                         for (i = 0; i < dhd->pktfilter_count; i++) {
1339                                 dhd_pktfilter_offload_set(dhd,
1340                                                           dhd->pktfilter[i]);
1341                                 dhd_pktfilter_offload_enable(dhd,
1342                                      dhd->pktfilter[i],
1343                                      dhd_pkt_filter_init,
1344                                      dhd_master_mode);
1345                         }
1346                 }
1347         }
1348 #endif                          /* PKT_FILTER_SUPPORT */
1349
1350         dhd_os_proto_unblock(dhd);
1351
1352         return 0;
1353 }
1354
1355 #ifdef SIMPLE_ISCAN
1356 uint iscan_thread_id;
1357 iscan_buf_t *iscan_chain;
1358
1359 iscan_buf_t *dhd_iscan_allocate_buf(dhd_pub_t *dhd, iscan_buf_t **iscanbuf)
1360 {
1361         iscan_buf_t *iscanbuf_alloc = 0;
1362         iscan_buf_t *iscanbuf_head;
1363
1364         dhd_iscan_lock();
1365
1366         iscanbuf_alloc = kmalloc(sizeof(iscan_buf_t), GFP_ATOMIC);
1367         if (iscanbuf_alloc == NULL)
1368                 goto fail;
1369
1370         iscanbuf_alloc->next = NULL;
1371         iscanbuf_head = *iscanbuf;
1372
1373         DHD_ISCAN(("%s: addr of allocated node = 0x%X"
1374                    "addr of iscanbuf_head = 0x%X dhd = 0x%X\n",
1375                    __func__, iscanbuf_alloc, iscanbuf_head, dhd));
1376
1377         if (iscanbuf_head == NULL) {
1378                 *iscanbuf = iscanbuf_alloc;
1379                 DHD_ISCAN(("%s: Head is allocated\n", __func__));
1380                 goto fail;
1381         }
1382
1383         while (iscanbuf_head->next)
1384                 iscanbuf_head = iscanbuf_head->next;
1385
1386         iscanbuf_head->next = iscanbuf_alloc;
1387
1388 fail:
1389         dhd_iscan_unlock();
1390         return iscanbuf_alloc;
1391 }
1392
1393 void dhd_iscan_free_buf(void *dhdp, iscan_buf_t *iscan_delete)
1394 {
1395         iscan_buf_t *iscanbuf_free = 0;
1396         iscan_buf_t *iscanbuf_prv = 0;
1397         iscan_buf_t *iscanbuf_cur = iscan_chain;
1398         dhd_pub_t *dhd = dhd_bus_pub(dhdp);
1399
1400         dhd_iscan_lock();
1401         /* If iscan_delete is null then delete the entire
1402          * chain or else delete specific one provided
1403          */
1404         if (!iscan_delete) {
1405                 while (iscanbuf_cur) {
1406                         iscanbuf_free = iscanbuf_cur;
1407                         iscanbuf_cur = iscanbuf_cur->next;
1408                         iscanbuf_free->next = 0;
1409                         kfree(iscanbuf_free);
1410                 }
1411                 iscan_chain = 0;
1412         } else {
1413                 while (iscanbuf_cur) {
1414                         if (iscanbuf_cur == iscan_delete)
1415                                 break;
1416                         iscanbuf_prv = iscanbuf_cur;
1417                         iscanbuf_cur = iscanbuf_cur->next;
1418                 }
1419                 if (iscanbuf_prv)
1420                         iscanbuf_prv->next = iscan_delete->next;
1421
1422                 iscan_delete->next = 0;
1423                 kfree(iscan_delete);
1424
1425                 if (!iscanbuf_prv)
1426                         iscan_chain = 0;
1427         }
1428         dhd_iscan_unlock();
1429 }
1430
1431 iscan_buf_t *dhd_iscan_result_buf(void)
1432 {
1433         return iscan_chain;
1434 }
1435
1436 /*
1437 * print scan cache
1438 * print partial iscan_skip list differently
1439 */
1440 int dhd_iscan_print_cache(iscan_buf_t *iscan_skip)
1441 {
1442         int i = 0, l = 0;
1443         iscan_buf_t *iscan_cur;
1444         wl_iscan_results_t *list;
1445         wl_scan_results_t *results;
1446         wl_bss_info_t UNALIGNED *bi;
1447
1448         dhd_iscan_lock();
1449
1450         iscan_cur = dhd_iscan_result_buf();
1451
1452         while (iscan_cur) {
1453                 list = (wl_iscan_results_t *)iscan_cur->iscan_buf;
1454                 if (!list)
1455                         break;
1456
1457                 results = (wl_scan_results_t *)&list->results;
1458                 if (!results)
1459                         break;
1460
1461                 if (results->version != WL_BSS_INFO_VERSION) {
1462                         DHD_ISCAN(("%s: results->version %d != "
1463                                 "WL_BSS_INFO_VERSION\n",
1464                                 __func__, results->version));
1465                         goto done;
1466                 }
1467
1468                 bi = results->bss_info;
1469                 for (i = 0; i < results->count; i++) {
1470                         if (!bi)
1471                                 break;
1472
1473                         DHD_ISCAN(("%s[%2.2d:%2.2d] %X:%X:%X:%X:%X:%X\n",
1474                                    iscan_cur != iscan_skip ? "BSS" : "bss", l,
1475                                    i, bi->BSSID.octet[0], bi->BSSID.octet[1],
1476                                    bi->BSSID.octet[2], bi->BSSID.octet[3],
1477                                    bi->BSSID.octet[4], bi->BSSID.octet[5]));
1478
1479                         bi = (wl_bss_info_t *)((unsigned long)bi +
1480                                                 dtoh32(bi->length));
1481                 }
1482                 iscan_cur = iscan_cur->next;
1483                 l++;
1484         }
1485
1486 done:
1487         dhd_iscan_unlock();
1488         return 0;
1489 }
1490
1491 /*
1492 * delete disappeared AP from specific scan cache but skip partial
1493 * list in iscan_skip
1494 */
1495 int dhd_iscan_delete_bss(void *dhdp, void *addr, iscan_buf_t *iscan_skip)
1496 {
1497         int i = 0, j = 0, l = 0;
1498         iscan_buf_t *iscan_cur;
1499         wl_iscan_results_t *list;
1500         wl_scan_results_t *results;
1501         wl_bss_info_t UNALIGNED *bi, *bi_new, *bi_next;
1502
1503         unsigned char *s_addr = addr;
1504
1505         dhd_iscan_lock();
1506         DHD_ISCAN(("%s: BSS to remove %X:%X:%X:%X:%X:%X\n",
1507                    __func__, s_addr[0], s_addr[1], s_addr[2],
1508                    s_addr[3], s_addr[4], s_addr[5]));
1509
1510         iscan_cur = dhd_iscan_result_buf();
1511
1512         while (iscan_cur) {
1513                 if (iscan_cur != iscan_skip) {
1514                         list = (wl_iscan_results_t *)iscan_cur->iscan_buf;
1515                         if (!list)
1516                                 break;
1517
1518                         results = (wl_scan_results_t *)&list->results;
1519                         if (!results)
1520                                 break;
1521
1522                         if (results->version != WL_BSS_INFO_VERSION) {
1523                                 DHD_ERROR(("%s: results->version %d != "
1524                                         "WL_BSS_INFO_VERSION\n",
1525                                         __func__, results->version));
1526                                 goto done;
1527                         }
1528
1529                         bi = results->bss_info;
1530                         for (i = 0; i < results->count; i++) {
1531                                 if (!bi)
1532                                         break;
1533
1534                                 if (!memcmp
1535                                     (bi->BSSID.octet, addr, ETHER_ADDR_LEN)) {
1536                                         DHD_ISCAN(("%s: Del BSS[%2.2d:%2.2d] "
1537                                         "%X:%X:%X:%X:%X:%X\n",
1538                                         __func__, l, i, bi->BSSID.octet[0],
1539                                         bi->BSSID.octet[1], bi->BSSID.octet[2],
1540                                         bi->BSSID.octet[3], bi->BSSID.octet[4],
1541                                         bi->BSSID.octet[5]));
1542
1543                                         bi_new = bi;
1544                                         bi = (wl_bss_info_t *)((unsigned long)bi +
1545                                                                 dtoh32
1546                                                                 (bi->length));
1547 /*
1548                         if(bi && bi_new) {
1549                                 bcopy(bi, bi_new, results->buflen -
1550                                 dtoh32(bi_new->length));
1551                                 results->buflen -= dtoh32(bi_new->length);
1552                         }
1553 */
1554                                         results->buflen -=
1555                                             dtoh32(bi_new->length);
1556                                         results->count--;
1557
1558                                         for (j = i; j < results->count; j++) {
1559                                                 if (bi && bi_new) {
1560                                                         DHD_ISCAN(("%s: Moved up BSS[%2.2d:%2.2d]" "%X:%X:%X:%X:%X:%X\n",
1561                                                         __func__, l, j,
1562                                                         bi->BSSID.octet[0],
1563                                                         bi->BSSID.octet[1],
1564                                                         bi->BSSID.octet[2],
1565                                                         bi->BSSID.octet[3],
1566                                                         bi->BSSID.octet[4],
1567                                                         bi->BSSID.octet[5]));
1568
1569                                                         bi_next =
1570                                                             (wl_bss_info_t *)((unsigned long)bi +
1571                                                                  dtoh32
1572                                                                  (bi->length));
1573                                                         bcopy(bi, bi_new,
1574                                                               dtoh32
1575                                                               (bi->length));
1576                                                         bi_new =
1577                                                             (wl_bss_info_t *)((unsigned long)bi_new +
1578                                                                  dtoh32
1579                                                                  (bi_new->
1580                                                                   length));
1581                                                         bi = bi_next;
1582                                                 }
1583                                         }
1584
1585                                         if (results->count == 0) {
1586                                                 /* Prune now empty partial
1587                                                 scan list */
1588                                                 dhd_iscan_free_buf(dhdp,
1589                                                                    iscan_cur);
1590                                                 goto done;
1591                                         }
1592                                         break;
1593                                 }
1594                                 bi = (wl_bss_info_t *)((unsigned long)bi +
1595                                                         dtoh32(bi->length));
1596                         }
1597                 }
1598                 iscan_cur = iscan_cur->next;
1599                 l++;
1600         }
1601
1602 done:
1603         dhd_iscan_unlock();
1604         return 0;
1605 }
1606
1607 int dhd_iscan_remove_duplicates(void *dhdp, iscan_buf_t *iscan_cur)
1608 {
1609         int i = 0;
1610         wl_iscan_results_t *list;
1611         wl_scan_results_t *results;
1612         wl_bss_info_t UNALIGNED *bi, *bi_new, *bi_next;
1613
1614         dhd_iscan_lock();
1615
1616         DHD_ISCAN(("%s: Scan cache before delete\n", __func__));
1617         dhd_iscan_print_cache(iscan_cur);
1618
1619         if (!iscan_cur)
1620                 goto done;
1621
1622         list = (wl_iscan_results_t *)iscan_cur->iscan_buf;
1623         if (!list)
1624                 goto done;
1625
1626         results = (wl_scan_results_t *)&list->results;
1627         if (!results)
1628                 goto done;
1629
1630         if (results->version != WL_BSS_INFO_VERSION) {
1631                 DHD_ERROR(("%s: results->version %d != WL_BSS_INFO_VERSION\n",
1632                            __func__, results->version));
1633                 goto done;
1634         }
1635
1636         bi = results->bss_info;
1637         for (i = 0; i < results->count; i++) {
1638                 if (!bi)
1639                         break;
1640
1641                 DHD_ISCAN(("%s: Find dups for BSS[%2.2d] %X:%X:%X:%X:%X:%X\n",
1642                            __func__, i, bi->BSSID.octet[0],
1643                            bi->BSSID.octet[1], bi->BSSID.octet[2],
1644                            bi->BSSID.octet[3], bi->BSSID.octet[4],
1645                            bi->BSSID.octet[5]));
1646
1647                 dhd_iscan_delete_bss(dhdp, bi->BSSID.octet, iscan_cur);
1648
1649                 bi = (wl_bss_info_t *)((unsigned long)bi + dtoh32(bi->length));
1650         }
1651
1652 done:
1653         DHD_ISCAN(("%s: Scan cache after delete\n", __func__));
1654         dhd_iscan_print_cache(iscan_cur);
1655         dhd_iscan_unlock();
1656         return 0;
1657 }
1658
1659 void dhd_iscan_ind_scan_confirm(void *dhdp, bool status)
1660 {
1661
1662         dhd_ind_scan_confirm(dhdp, status);
1663 }
1664
1665 int dhd_iscan_request(void *dhdp, u16 action)
1666 {
1667         int rc;
1668         wl_iscan_params_t params;
1669         dhd_pub_t *dhd = dhd_bus_pub(dhdp);
1670         char buf[WLC_IOCTL_SMLEN];
1671
1672         memset(&params, 0, sizeof(wl_iscan_params_t));
1673         memcpy(&params.params.bssid, &ether_bcast, ETHER_ADDR_LEN);
1674
1675         params.params.bss_type = DOT11_BSSTYPE_ANY;
1676         params.params.scan_type = DOT11_SCANTYPE_ACTIVE;
1677
1678         params.params.nprobes = htod32(-1);
1679         params.params.active_time = htod32(-1);
1680         params.params.passive_time = htod32(-1);
1681         params.params.home_time = htod32(-1);
1682         params.params.channel_num = htod32(0);
1683
1684         params.version = htod32(ISCAN_REQ_VERSION);
1685         params.action = htod16(action);
1686         params.scan_duration = htod16(0);
1687
1688         bcm_mkiovar("iscan", (char *)&params, sizeof(wl_iscan_params_t), buf,
1689                     WLC_IOCTL_SMLEN);
1690         rc = dhd_wl_ioctl(dhdp, WLC_SET_VAR, buf, WLC_IOCTL_SMLEN);
1691
1692         return rc;
1693 }
1694
1695 static int dhd_iscan_get_partial_result(void *dhdp, uint *scan_count)
1696 {
1697         wl_iscan_results_t *list_buf;
1698         wl_iscan_results_t list;
1699         wl_scan_results_t *results;
1700         iscan_buf_t *iscan_cur;
1701         int status = -1;
1702         dhd_pub_t *dhd = dhd_bus_pub(dhdp);
1703         int rc;
1704
1705         iscan_cur = dhd_iscan_allocate_buf(dhd, &iscan_chain);
1706         if (!iscan_cur) {
1707                 DHD_ERROR(("%s: Failed to allocate node\n", __func__));
1708                 dhd_iscan_free_buf(dhdp, 0);
1709                 dhd_iscan_request(dhdp, WL_SCAN_ACTION_ABORT);
1710                 goto fail;
1711         }
1712
1713         dhd_iscan_lock();
1714
1715         memset(iscan_cur->iscan_buf, 0, WLC_IW_ISCAN_MAXLEN);
1716         list_buf = (wl_iscan_results_t *) iscan_cur->iscan_buf;
1717         results = &list_buf->results;
1718         results->buflen = WL_ISCAN_RESULTS_FIXED_SIZE;
1719         results->version = 0;
1720         results->count = 0;
1721
1722         memset(&list, 0, sizeof(list));
1723         list.results.buflen = htod32(WLC_IW_ISCAN_MAXLEN);
1724         bcm_mkiovar("iscanresults", (char *)&list, WL_ISCAN_RESULTS_FIXED_SIZE,
1725                     iscan_cur->iscan_buf, WLC_IW_ISCAN_MAXLEN);
1726         rc = dhd_wl_ioctl(dhdp, WLC_GET_VAR, iscan_cur->iscan_buf,
1727                           WLC_IW_ISCAN_MAXLEN);
1728
1729         results->buflen = dtoh32(results->buflen);
1730         results->version = dtoh32(results->version);
1731         *scan_count = results->count = dtoh32(results->count);
1732         status = dtoh32(list_buf->status);
1733
1734         dhd_iscan_unlock();
1735
1736         if (!(*scan_count))
1737                 dhd_iscan_free_buf(dhdp, iscan_cur);
1738         else
1739                 dhd_iscan_remove_duplicates(dhdp, iscan_cur);
1740
1741 fail:
1742         return status;
1743 }
1744 #endif                          /* SIMPLE_ISCAN */
1745
1746 #ifdef PNO_SUPPORT
1747 int dhd_pno_clean(dhd_pub_t *dhd)
1748 {
1749         char iovbuf[128];
1750         int pfn_enabled = 0;
1751         int iov_len = 0;
1752         int ret;
1753
1754         /* Disable pfn */
1755         iov_len =
1756             bcm_mkiovar("pfn", (char *)&pfn_enabled, 4, iovbuf, sizeof(iovbuf));
1757         ret = dhdcdc_set_ioctl(dhd, 0, WLC_SET_VAR, iovbuf, sizeof(iovbuf));
1758         if (ret >= 0) {
1759                 /* clear pfn */
1760                 iov_len = bcm_mkiovar("pfnclear", 0, 0, iovbuf, sizeof(iovbuf));
1761                 if (iov_len) {
1762                         ret = dhdcdc_set_ioctl(dhd, 0, WLC_SET_VAR, iovbuf,
1763                                         iov_len);
1764                         if (ret < 0) {
1765                                 DHD_ERROR(("%s failed code %d\n", __func__,
1766                                            ret));
1767                         }
1768                 } else {
1769                         ret = -1;
1770                         DHD_ERROR(("%s failed code %d\n", __func__, iov_len));
1771                 }
1772         } else
1773                 DHD_ERROR(("%s failed code %d\n", __func__, ret));
1774
1775         return ret;
1776 }
1777
1778 int dhd_pno_enable(dhd_pub_t *dhd, int pfn_enabled)
1779 {
1780         char iovbuf[128];
1781         int ret = -1;
1782
1783         if ((!dhd) && ((pfn_enabled != 0) || (pfn_enabled != 1))) {
1784                 DHD_ERROR(("%s error exit\n", __func__));
1785                 return ret;
1786         }
1787
1788         /* Enable/disable PNO */
1789         ret = bcm_mkiovar("pfn", (char *)&pfn_enabled, 4, iovbuf,
1790                         sizeof(iovbuf));
1791         if (ret > 0) {
1792                 ret = dhdcdc_set_ioctl(dhd, 0, WLC_SET_VAR, iovbuf,
1793                                 sizeof(iovbuf));
1794                 if (ret < 0) {
1795                         DHD_ERROR(("%s failed for error=%d\n", __func__, ret));
1796                         return ret;
1797                 } else {
1798                         dhd->pno_enable = pfn_enabled;
1799                         DHD_TRACE(("%s set pno as %d\n", __func__,
1800                                    dhd->pno_enable));
1801                 }
1802         } else
1803                 DHD_ERROR(("%s failed err=%d\n", __func__, ret));
1804
1805         return ret;
1806 }
1807
1808 /* Function to execute combined scan */
1809 int
1810 dhd_pno_set(dhd_pub_t *dhd, wlc_ssid_t *ssids_local, int nssid, unsigned char scan_fr)
1811 {
1812         int err = -1;
1813         char iovbuf[128];
1814         int k, i;
1815         wl_pfn_param_t pfn_param;
1816         wl_pfn_t pfn_element;
1817
1818         DHD_TRACE(("%s nssid=%d nchan=%d\n", __func__, nssid, scan_fr));
1819
1820         if ((!dhd) && (!ssids_local)) {
1821                 DHD_ERROR(("%s error exit\n", __func__));
1822                 err = -1;
1823         }
1824
1825         /* Check for broadcast ssid */
1826         for (k = 0; k < nssid; k++) {
1827                 if (!ssids_local[k].SSID_len) {
1828                         DHD_ERROR(("%d: Broadcast SSID is ilegal for PNO "
1829                                 "setting\n", k));
1830                         return err;
1831                 }
1832         }
1833 /* #define  PNO_DUMP 1 */
1834 #ifdef PNO_DUMP
1835         {
1836                 int j;
1837                 for (j = 0; j < nssid; j++) {
1838                         DHD_ERROR(("%d: scan  for  %s size =%d\n", j,
1839                                    ssids_local[j].SSID,
1840                                    ssids_local[j].SSID_len));
1841                 }
1842         }
1843 #endif                          /* PNO_DUMP */
1844
1845         /* clean up everything */
1846         err = dhd_pno_clean(dhd);
1847         if (err < 0) {
1848                 DHD_ERROR(("%s failed error=%d\n", __func__, err));
1849                 return err;
1850         }
1851         memset(&pfn_param, 0, sizeof(pfn_param));
1852         memset(&pfn_element, 0, sizeof(pfn_element));
1853
1854         /* set pfn parameters */
1855         pfn_param.version = htod32(PFN_VERSION);
1856         pfn_param.flags = htod16((PFN_LIST_ORDER << SORT_CRITERIA_BIT));
1857
1858         /* set up pno scan fr */
1859         if (scan_fr != 0)
1860                 pfn_param.scan_freq = htod32(scan_fr);
1861
1862         bcm_mkiovar("pfn_set", (char *)&pfn_param, sizeof(pfn_param), iovbuf,
1863                     sizeof(iovbuf));
1864         dhdcdc_set_ioctl(dhd, 0, WLC_SET_VAR, iovbuf, sizeof(iovbuf));
1865
1866         /* set all pfn ssid */
1867         for (i = 0; i < nssid; i++) {
1868
1869                 pfn_element.bss_type = htod32(DOT11_BSSTYPE_INFRASTRUCTURE);
1870                 pfn_element.auth = (DOT11_OPEN_SYSTEM);
1871                 pfn_element.wpa_auth = htod32(WPA_AUTH_PFN_ANY);
1872                 pfn_element.wsec = htod32(0);
1873                 pfn_element.infra = htod32(1);
1874
1875                 memcpy((char *)pfn_element.ssid.SSID, ssids_local[i].SSID,
1876                        ssids_local[i].SSID_len);
1877                 pfn_element.ssid.SSID_len = ssids_local[i].SSID_len;
1878
1879                 err = bcm_mkiovar("pfn_add", (char *)&pfn_element,
1880                                 sizeof(pfn_element), iovbuf, sizeof(iovbuf));
1881                 if (err > 0) {
1882                         err = dhdcdc_set_ioctl(dhd, 0, WLC_SET_VAR, iovbuf,
1883                                         sizeof(iovbuf));
1884                         if (err < 0) {
1885                                 DHD_ERROR(("%s failed for i=%d error=%d\n",
1886                                            __func__, i, err));
1887                                 return err;
1888                         }
1889                 } else
1890                         DHD_ERROR(("%s failed err=%d\n", __func__, err));
1891         }
1892
1893         /* Enable PNO */
1894         /* dhd_pno_enable(dhd, 1); */
1895         return err;
1896 }
1897
1898 int dhd_pno_get_status(dhd_pub_t *dhd)
1899 {
1900         int ret = -1;
1901
1902         if (!dhd)
1903                 return ret;
1904         else
1905                 return dhd->pno_enable;
1906 }
1907
1908 #endif                          /* PNO_SUPPORT */
1909
1910 /* Androd ComboSCAN support */