HTC Vision (and others) have a Broadcom BCM4329 chip-set for WiFi/Bluetooth/FM-rx...
[bootloader-ap.git] / drivers / net / wireless / bcm4329 / dhd_common.c
diff --git a/drivers/net/wireless/bcm4329/dhd_common.c b/drivers/net/wireless/bcm4329/dhd_common.c
new file mode 100644 (file)
index 0000000..a0e6a73
--- /dev/null
@@ -0,0 +1,894 @@
+/*
+ * Broadcom Dongle Host Driver (DHD), common DHD core.
+ *
+ * Copyright (C) 1999-2010, Broadcom Corporation
+ * 
+ *      Unless you and Broadcom execute a separate written software license
+ * agreement governing use of this software, this software is licensed to you
+ * under the terms of the GNU General Public License version 2 (the "GPL"),
+ * available at http://www.broadcom.com/licenses/GPLv2.php, with the
+ * following added to such license:
+ * 
+ *      As a special exception, the copyright holders of this software give you
+ * permission to link this software with independent modules, and to copy and
+ * distribute the resulting executable under terms of your choice, provided that
+ * you also meet, for each linked independent module, the terms and conditions of
+ * the license of that module.  An independent module is a module which is not
+ * derived from this software.  The special exception does not apply to any
+ * modifications of the software.
+ * 
+ *      Notwithstanding the above, under no circumstances may you combine this
+ * software in any way with any other Broadcom software provided under a license
+ * other than the GPL, without Broadcom's express prior written consent.
+ *
+ * $Id: dhd_common.c,v 1.5.6.8.2.6.6.41 2010/02/24 01:52:41 Exp $
+ */
+#include <typedefs.h>
+#include <osl.h>
+
+#include <epivers.h>
+#include <bcmutils.h>
+
+#include <bcmendian.h>
+#include <dngl_stats.h>
+#include <dhd.h>
+#include <dhd_bus.h>
+#include <dhd_proto.h>
+#include <dhd_dbg.h>
+#include <msgtrace.h>
+
+
+int dhd_msg_level;
+
+char fw_path[MOD_PARAM_PATHLEN];
+char nv_path[MOD_PARAM_PATHLEN];
+
+/* Last connection success/failure status */
+uint32 dhd_conn_event;
+uint32 dhd_conn_status;
+uint32 dhd_conn_reason;
+
+#ifdef DHD_DEBUG
+const char dhd_version[] = "Dongle Host Driver, version " EPI_VERSION_STR "\nCompiled on "
+       __DATE__ " at " __TIME__;
+#else
+const char dhd_version[] = "Dongle Host Driver, version " EPI_VERSION_STR;
+#endif
+
+void dhd_set_timer(void *bus, uint wdtick);
+
+
+/* IOVar table */
+enum {
+       IOV_VERSION = 1,
+       IOV_MSGLEVEL,
+       IOV_BCMERRORSTR,
+       IOV_BCMERROR,
+       IOV_WDTICK,
+       IOV_DUMP,
+       IOV_CLEARCOUNTS,
+       IOV_LOGDUMP,
+       IOV_LOGCAL,
+       IOV_LOGSTAMP,
+       IOV_GPIOOB,
+       IOV_IOCTLTIMEOUT,
+       IOV_LAST
+};
+
+const bcm_iovar_t dhd_iovars[] = {
+       {"version",     IOV_VERSION,    0,      IOVT_BUFFER,    sizeof(dhd_version) },
+#ifdef DHD_DEBUG
+       {"msglevel",    IOV_MSGLEVEL,   0,      IOVT_UINT32,    0 },
+#endif /* DHD_DEBUG */
+       {"bcmerrorstr", IOV_BCMERRORSTR, 0, IOVT_BUFFER,        BCME_STRLEN },
+       {"bcmerror",    IOV_BCMERROR,   0,      IOVT_INT8,      0 },
+       {"wdtick",      IOV_WDTICK, 0,  IOVT_UINT32,    0 },
+       {"dump",        IOV_DUMP,       0,      IOVT_BUFFER,    DHD_IOCTL_MAXLEN },
+       {"clearcounts", IOV_CLEARCOUNTS, 0, IOVT_VOID,  0 },
+       {"gpioob",      IOV_GPIOOB,     0,      IOVT_UINT32,    0 },
+       {"ioctl_timeout",       IOV_IOCTLTIMEOUT,       0,      IOVT_UINT32,    0 },
+       {NULL, 0, 0, 0, 0 }
+};
+
+void
+dhd_common_init(void)
+{
+       /* Init global variables at run-time, not as part of the declaration.
+        * This is required to support init/de-init of the driver. Initialization
+        * of globals as part of the declaration results in non-deterministic
+        * behaviour since the value of the globals may be different on the
+        * first time that the driver is initialized vs subsequent initializations.
+        */
+       dhd_msg_level = DHD_ERROR_VAL;
+#ifdef CONFIG_BCM4329_FW_PATH
+       strncpy(fw_path, CONFIG_BCM4329_FW_PATH, MOD_PARAM_PATHLEN-1);
+#else
+       fw_path[0] = '\0';
+#endif
+#ifdef CONFIG_BCM4329_NVRAM_PATH
+       strncpy(nv_path, CONFIG_BCM4329_NVRAM_PATH, MOD_PARAM_PATHLEN-1);
+#else
+       nv_path[0] = '\0';
+#endif
+}
+
+static int
+dhd_dump(dhd_pub_t *dhdp, char *buf, int buflen)
+{
+       char eabuf[ETHER_ADDR_STR_LEN];
+
+       struct bcmstrbuf b;
+       struct bcmstrbuf *strbuf = &b;
+
+       bcm_binit(strbuf, buf, buflen);
+
+       /* Base DHD info */
+       bcm_bprintf(strbuf, "%s\n", dhd_version);
+       bcm_bprintf(strbuf, "\n");
+       bcm_bprintf(strbuf, "pub.up %d pub.txoff %d pub.busstate %d\n",
+                   dhdp->up, dhdp->txoff, dhdp->busstate);
+       bcm_bprintf(strbuf, "pub.hdrlen %d pub.maxctl %d pub.rxsz %d\n",
+                   dhdp->hdrlen, dhdp->maxctl, dhdp->rxsz);
+       bcm_bprintf(strbuf, "pub.iswl %d pub.drv_version %ld pub.mac %s\n",
+                   dhdp->iswl, dhdp->drv_version, bcm_ether_ntoa(&dhdp->mac, eabuf));
+       bcm_bprintf(strbuf, "pub.bcmerror %d tickcnt %d\n", dhdp->bcmerror, dhdp->tickcnt);
+
+       bcm_bprintf(strbuf, "dongle stats:\n");
+       bcm_bprintf(strbuf, "tx_packets %ld tx_bytes %ld tx_errors %ld tx_dropped %ld\n",
+                   dhdp->dstats.tx_packets, dhdp->dstats.tx_bytes,
+                   dhdp->dstats.tx_errors, dhdp->dstats.tx_dropped);
+       bcm_bprintf(strbuf, "rx_packets %ld rx_bytes %ld rx_errors %ld rx_dropped %ld\n",
+                   dhdp->dstats.rx_packets, dhdp->dstats.rx_bytes,
+                   dhdp->dstats.rx_errors, dhdp->dstats.rx_dropped);
+       bcm_bprintf(strbuf, "multicast %ld\n", dhdp->dstats.multicast);
+
+       bcm_bprintf(strbuf, "bus stats:\n");
+       bcm_bprintf(strbuf, "tx_packets %ld tx_multicast %ld tx_errors %ld\n",
+                   dhdp->tx_packets, dhdp->tx_multicast, dhdp->tx_errors);
+       bcm_bprintf(strbuf, "tx_ctlpkts %ld tx_ctlerrs %ld\n",
+                   dhdp->tx_ctlpkts, dhdp->tx_ctlerrs);
+       bcm_bprintf(strbuf, "rx_packets %ld rx_multicast %ld rx_errors %ld \n",
+                   dhdp->rx_packets, dhdp->rx_multicast, dhdp->rx_errors);
+       bcm_bprintf(strbuf, "rx_ctlpkts %ld rx_ctlerrs %ld rx_dropped %ld rx_flushed %ld\n",
+                   dhdp->rx_ctlpkts, dhdp->rx_ctlerrs, dhdp->rx_dropped, dhdp->rx_flushed);
+       bcm_bprintf(strbuf, "rx_readahead_cnt %ld tx_realloc %ld fc_packets %ld\n",
+                   dhdp->rx_readahead_cnt, dhdp->tx_realloc, dhdp->fc_packets);
+       bcm_bprintf(strbuf, "wd_dpc_sched %ld\n", dhdp->wd_dpc_sched);
+       bcm_bprintf(strbuf, "\n");
+
+       /* Add any prot info */
+       dhd_prot_dump(dhdp, strbuf);
+       bcm_bprintf(strbuf, "\n");
+
+       /* Add any bus info */
+       dhd_bus_dump(dhdp, strbuf);
+
+       return (!strbuf->size ? BCME_BUFTOOSHORT : 0);
+}
+
+static int
+dhd_doiovar(dhd_pub_t *dhd_pub, const bcm_iovar_t *vi, uint32 actionid, const char *name,
+            void *params, int plen, void *arg, int len, int val_size)
+{
+       int bcmerror = 0;
+       int32 int_val = 0;
+
+       DHD_TRACE(("%s: Enter\n", __FUNCTION__));
+
+       if ((bcmerror = bcm_iovar_lencheck(vi, arg, len, IOV_ISSET(actionid))) != 0)
+               goto exit;
+
+       if (plen >= (int)sizeof(int_val))
+               bcopy(params, &int_val, sizeof(int_val));
+
+       switch (actionid) {
+       case IOV_GVAL(IOV_VERSION):
+               /* Need to have checked buffer length */
+               strncpy((char*)arg, dhd_version, len);
+               break;
+
+       case IOV_GVAL(IOV_MSGLEVEL):
+               int_val = (int32)dhd_msg_level;
+               bcopy(&int_val, arg, val_size);
+               break;
+
+       case IOV_SVAL(IOV_MSGLEVEL):
+               dhd_msg_level = int_val;
+               break;
+
+       case IOV_GVAL(IOV_BCMERRORSTR):
+               strncpy((char *)arg, bcmerrorstr(dhd_pub->bcmerror), BCME_STRLEN);
+               ((char *)arg)[BCME_STRLEN - 1] = 0x00;
+               break;
+
+       case IOV_GVAL(IOV_BCMERROR):
+               int_val = (int32)dhd_pub->bcmerror;
+               bcopy(&int_val, arg, val_size);
+               break;
+
+       case IOV_GVAL(IOV_WDTICK):
+               int_val = (int32)dhd_watchdog_ms;
+               bcopy(&int_val, arg, val_size);
+               break;
+
+       case IOV_SVAL(IOV_WDTICK):
+               if (!dhd_pub->up) {
+                       bcmerror = BCME_NOTUP;
+                       break;
+               }
+               dhd_os_wd_timer(dhd_pub, (uint)int_val);
+               break;
+
+       case IOV_GVAL(IOV_DUMP):
+               bcmerror = dhd_dump(dhd_pub, arg, len);
+               break;
+
+
+       case IOV_SVAL(IOV_CLEARCOUNTS):
+               dhd_pub->tx_packets = dhd_pub->rx_packets = 0;
+               dhd_pub->tx_errors = dhd_pub->rx_errors = 0;
+               dhd_pub->tx_ctlpkts = dhd_pub->rx_ctlpkts = 0;
+               dhd_pub->tx_ctlerrs = dhd_pub->rx_ctlerrs = 0;
+               dhd_pub->rx_dropped = 0;
+               dhd_pub->rx_readahead_cnt = 0;
+               dhd_pub->tx_realloc = 0;
+               dhd_pub->wd_dpc_sched = 0;
+               memset(&dhd_pub->dstats, 0, sizeof(dhd_pub->dstats));
+               dhd_bus_clearcounts(dhd_pub);
+               break;
+
+
+       case IOV_GVAL(IOV_IOCTLTIMEOUT): {
+               int_val = (int32)dhd_os_get_ioctl_resp_timeout();
+               bcopy(&int_val, arg, sizeof(int_val));
+               break;
+       }
+
+       case IOV_SVAL(IOV_IOCTLTIMEOUT): {
+               if (int_val <= 0)
+                       bcmerror = BCME_BADARG;
+               else
+                       dhd_os_set_ioctl_resp_timeout((unsigned int)int_val);
+               break;
+       }
+
+
+       default:
+               bcmerror = BCME_UNSUPPORTED;
+               break;
+       }
+
+exit:
+       return bcmerror;
+}
+
+/* Store the status of a connection attempt for later retrieval by an iovar */
+void
+dhd_store_conn_status(uint32 event, uint32 status, uint32 reason)
+{
+       /* Do not overwrite a WLC_E_PRUNE with a WLC_E_SET_SSID
+        * because an encryption/rsn mismatch results in both events, and
+        * the important information is in the WLC_E_PRUNE.
+        */
+       if (!(event == WLC_E_SET_SSID && status == WLC_E_STATUS_FAIL &&
+             dhd_conn_event == WLC_E_PRUNE)) {
+               dhd_conn_event = event;
+               dhd_conn_status = status;
+               dhd_conn_reason = reason;
+       }
+}
+
+bool
+dhd_prec_enq(dhd_pub_t *dhdp, struct pktq *q, void *pkt, int prec)
+{
+       void *p;
+       int eprec = -1;         /* precedence to evict from */
+       bool discard_oldest;
+
+       /* Fast case, precedence queue is not full and we are also not
+        * exceeding total queue length
+        */
+       if (!pktq_pfull(q, prec) && !pktq_full(q)) {
+               pktq_penq(q, prec, pkt);
+               return TRUE;
+       }
+
+       /* Determine precedence from which to evict packet, if any */
+       if (pktq_pfull(q, prec))
+               eprec = prec;
+       else if (pktq_full(q)) {
+               p = pktq_peek_tail(q, &eprec);
+               ASSERT(p);
+               if (eprec > prec)
+                       return FALSE;
+       }
+
+       /* Evict if needed */
+       if (eprec >= 0) {
+               /* Detect queueing to unconfigured precedence */
+               ASSERT(!pktq_pempty(q, eprec));
+               discard_oldest = AC_BITMAP_TST(dhdp->wme_dp, eprec);
+               if (eprec == prec && !discard_oldest)
+                       return FALSE;           /* refuse newer (incoming) packet */
+               /* Evict packet according to discard policy */
+               p = discard_oldest ? pktq_pdeq(q, eprec) : pktq_pdeq_tail(q, eprec);
+               if (p == NULL) {
+                       DHD_ERROR(("%s: pktq_penq() failed, oldest %d.",
+                               __FUNCTION__, discard_oldest));
+                       ASSERT(p);
+               }
+
+               PKTFREE(dhdp->osh, p, TRUE);
+       }
+
+       /* Enqueue */
+       p = pktq_penq(q, prec, pkt);
+       if (p == NULL) {
+               DHD_ERROR(("%s: pktq_penq() failed.", __FUNCTION__));
+               ASSERT(p);
+       }
+
+       return TRUE;
+}
+
+static int
+dhd_iovar_op(dhd_pub_t *dhd_pub, const char *name,
+             void *params, int plen, void *arg, int len, bool set)
+{
+       int bcmerror = 0;
+       int val_size;
+       const bcm_iovar_t *vi = NULL;
+       uint32 actionid;
+
+       DHD_TRACE(("%s: Enter\n", __FUNCTION__));
+
+       ASSERT(name);
+       ASSERT(len >= 0);
+
+       /* Get MUST have return space */
+       ASSERT(set || (arg && len));
+
+       /* Set does NOT take qualifiers */
+       ASSERT(!set || (!params && !plen));
+
+       if ((vi = bcm_iovar_lookup(dhd_iovars, name)) == NULL) {
+               bcmerror = BCME_UNSUPPORTED;
+               goto exit;
+       }
+
+       DHD_CTL(("%s: %s %s, len %d plen %d\n", __FUNCTION__,
+                name, (set ? "set" : "get"), len, plen));
+
+       /* set up 'params' pointer in case this is a set command so that
+        * the convenience int and bool code can be common to set and get
+        */
+       if (params == NULL) {
+               params = arg;
+               plen = len;
+       }
+
+       if (vi->type == IOVT_VOID)
+               val_size = 0;
+       else if (vi->type == IOVT_BUFFER)
+               val_size = len;
+       else
+               /* all other types are integer sized */
+               val_size = sizeof(int);
+
+       actionid = set ? IOV_SVAL(vi->varid) : IOV_GVAL(vi->varid);
+       bcmerror = dhd_doiovar(dhd_pub, vi, actionid, name, params, plen, arg, len, val_size);
+
+exit:
+       return bcmerror;
+}
+
+int
+dhd_ioctl(dhd_pub_t *dhd_pub, dhd_ioctl_t *ioc, void *buf, uint buflen)
+{
+       int bcmerror = 0;
+
+       DHD_TRACE(("%s: Enter\n", __FUNCTION__));
+
+       if (!buf) return BCME_BADARG;
+
+       switch (ioc->cmd) {
+       case DHD_GET_MAGIC:
+               if (buflen < sizeof(int))
+                       bcmerror = BCME_BUFTOOSHORT;
+               else
+                       *(int*)buf = DHD_IOCTL_MAGIC;
+               break;
+
+       case DHD_GET_VERSION:
+               if (buflen < sizeof(int))
+                       bcmerror = -BCME_BUFTOOSHORT;
+               else
+                       *(int*)buf = DHD_IOCTL_VERSION;
+               break;
+
+       case DHD_GET_VAR:
+       case DHD_SET_VAR: {
+               char *arg;
+               uint arglen;
+
+               /* scan past the name to any arguments */
+               for (arg = buf, arglen = buflen; *arg && arglen; arg++, arglen--);
+
+               if (*arg) {
+                       bcmerror = BCME_BUFTOOSHORT;
+                       break;
+               }
+
+               /* account for the NUL terminator */
+               arg++, arglen--;
+
+               /* call with the appropriate arguments */
+               if (ioc->cmd == DHD_GET_VAR)
+                       bcmerror = dhd_iovar_op(dhd_pub, buf, arg, arglen,
+                       buf, buflen, IOV_GET);
+               else
+                       bcmerror = dhd_iovar_op(dhd_pub, buf, NULL, 0, arg, arglen, IOV_SET);
+               if (bcmerror != BCME_UNSUPPORTED)
+                       break;
+
+               /* not in generic table, try protocol module */
+               if (ioc->cmd == DHD_GET_VAR)
+                       bcmerror = dhd_prot_iovar_op(dhd_pub, buf, arg,
+                                                    arglen, buf, buflen, IOV_GET);
+               else
+                       bcmerror = dhd_prot_iovar_op(dhd_pub, buf,
+                                                    NULL, 0, arg, arglen, IOV_SET);
+               if (bcmerror != BCME_UNSUPPORTED)
+                       break;
+
+               /* if still not found, try bus module */
+               if (ioc->cmd == DHD_GET_VAR)
+                       bcmerror = dhd_bus_iovar_op(dhd_pub, buf,
+                                                   arg, arglen, buf, buflen, IOV_GET);
+               else
+                       bcmerror = dhd_bus_iovar_op(dhd_pub, buf,
+                                                   NULL, 0, arg, arglen, IOV_SET);
+
+               break;
+       }
+
+       default:
+               bcmerror = BCME_UNSUPPORTED;
+       }
+
+       return bcmerror;
+}
+
+#ifdef APSTA_PINGTEST
+struct ether_addr guest_eas[MAX_GUEST];
+#endif
+
+#ifdef SHOW_EVENTS
+static void
+wl_show_host_event(wl_event_msg_t *event, void *event_data)
+{
+       uint i, status, reason;
+       bool group = FALSE, flush_txq = FALSE, link = FALSE;
+       char *auth_str, *event_name;
+       uchar *buf;
+       char err_msg[256], eabuf[ETHER_ADDR_STR_LEN];
+       static struct {uint event; char *event_name;} event_names[] = {
+               {WLC_E_SET_SSID, "SET_SSID"},
+               {WLC_E_JOIN, "JOIN"},
+               {WLC_E_START, "START"},
+               {WLC_E_AUTH, "AUTH"},
+               {WLC_E_AUTH_IND, "AUTH_IND"},
+               {WLC_E_DEAUTH, "DEAUTH"},
+               {WLC_E_DEAUTH_IND, "DEAUTH_IND"},
+               {WLC_E_ASSOC, "ASSOC"},
+               {WLC_E_ASSOC_IND, "ASSOC_IND"},
+               {WLC_E_REASSOC, "REASSOC"},
+               {WLC_E_REASSOC_IND, "REASSOC_IND"},
+               {WLC_E_DISASSOC, "DISASSOC"},
+               {WLC_E_DISASSOC_IND, "DISASSOC_IND"},
+               {WLC_E_QUIET_START, "START_QUIET"},
+               {WLC_E_QUIET_END, "END_QUIET"},
+               {WLC_E_BEACON_RX, "BEACON_RX"},
+               {WLC_E_LINK, "LINK"},
+               {WLC_E_MIC_ERROR, "MIC_ERROR"},
+               {WLC_E_NDIS_LINK, "NDIS_LINK"},
+               {WLC_E_ROAM, "ROAM"},
+               {WLC_E_TXFAIL, "TXFAIL"},
+               {WLC_E_PMKID_CACHE, "PMKID_CACHE"},
+               {WLC_E_RETROGRADE_TSF, "RETROGRADE_TSF"},
+               {WLC_E_PRUNE, "PRUNE"},
+               {WLC_E_AUTOAUTH, "AUTOAUTH"},
+               {WLC_E_EAPOL_MSG, "EAPOL_MSG"},
+               {WLC_E_SCAN_COMPLETE, "SCAN_COMPLETE"},
+               {WLC_E_ADDTS_IND, "ADDTS_IND"},
+               {WLC_E_DELTS_IND, "DELTS_IND"},
+               {WLC_E_BCNSENT_IND, "BCNSENT_IND"},
+               {WLC_E_BCNRX_MSG, "BCNRX_MSG"},
+               {WLC_E_BCNLOST_MSG, "BCNLOST_MSG"},
+               {WLC_E_ROAM_PREP, "ROAM_PREP"},
+               {WLC_E_PFN_NET_FOUND, "PNO_NET_FOUND"},
+               {WLC_E_PFN_NET_LOST, "PNO_NET_LOST"},
+               {WLC_E_RESET_COMPLETE, "RESET_COMPLETE"},
+               {WLC_E_JOIN_START, "JOIN_START"},
+               {WLC_E_ROAM_START, "ROAM_START"},
+               {WLC_E_ASSOC_START, "ASSOC_START"},
+               {WLC_E_IBSS_ASSOC, "IBSS_ASSOC"},
+               {WLC_E_RADIO, "RADIO"},
+               {WLC_E_PSM_WATCHDOG, "PSM_WATCHDOG"},
+               {WLC_E_PROBREQ_MSG, "PROBREQ_MSG"},
+               {WLC_E_SCAN_CONFIRM_IND, "SCAN_CONFIRM_IND"},
+               {WLC_E_PSK_SUP, "PSK_SUP"},
+               {WLC_E_COUNTRY_CODE_CHANGED, "COUNTRY_CODE_CHANGED"},
+               {WLC_E_EXCEEDED_MEDIUM_TIME, "EXCEEDED_MEDIUM_TIME"},
+               {WLC_E_ICV_ERROR, "ICV_ERROR"},
+               {WLC_E_UNICAST_DECODE_ERROR, "UNICAST_DECODE_ERROR"},
+               {WLC_E_MULTICAST_DECODE_ERROR, "MULTICAST_DECODE_ERROR"},
+               {WLC_E_TRACE, "TRACE"},
+               {WLC_E_ACTION_FRAME, "ACTION FRAME"},
+               {WLC_E_ACTION_FRAME_COMPLETE, "ACTION FRAME TX COMPLETE"},
+               {WLC_E_IF, "IF"},
+               {WLC_E_RSSI, "RSSI"},
+               {WLC_E_PFN_SCAN_COMPLETE, "SCAN_COMPLETE"}
+       };
+       uint event_type, flags, auth_type, datalen;
+       event_type = ntoh32(event->event_type);
+       flags = ntoh16(event->flags);
+       status = ntoh32(event->status);
+       reason = ntoh32(event->reason);
+       auth_type = ntoh32(event->auth_type);
+       datalen = ntoh32(event->datalen);
+       /* debug dump of event messages */
+       sprintf(eabuf, "%02x:%02x:%02x:%02x:%02x:%02x",
+               (uchar)event->addr.octet[0]&0xff,
+               (uchar)event->addr.octet[1]&0xff,
+               (uchar)event->addr.octet[2]&0xff,
+               (uchar)event->addr.octet[3]&0xff,
+               (uchar)event->addr.octet[4]&0xff,
+               (uchar)event->addr.octet[5]&0xff);
+
+       event_name = "UNKNOWN";
+       for (i = 0; i < ARRAYSIZE(event_names); i++) {
+               if (event_names[i].event == event_type)
+                       event_name = event_names[i].event_name;
+       }
+
+       DHD_EVENT(("EVENT: %s, event ID = %d\n", event_name, event_type));
+
+       if (flags & WLC_EVENT_MSG_LINK)
+               link = TRUE;
+       if (flags & WLC_EVENT_MSG_GROUP)
+               group = TRUE;
+       if (flags & WLC_EVENT_MSG_FLUSHTXQ)
+               flush_txq = TRUE;
+
+       switch (event_type) {
+       case WLC_E_START:
+       case WLC_E_DEAUTH:
+       case WLC_E_DISASSOC:
+               DHD_EVENT(("MACEVENT: %s, MAC %s\n", event_name, eabuf));
+               break;
+
+       case WLC_E_ASSOC_IND:
+       case WLC_E_REASSOC_IND:
+#ifdef APSTA_PINGTEST
+               {
+                       int i;
+                       for (i = 0; i < MAX_GUEST; ++i)
+                               if (ETHER_ISNULLADDR(&guest_eas[i]))
+                                       break;
+                       if (i < MAX_GUEST)
+                               bcopy(event->addr.octet, guest_eas[i].octet, ETHER_ADDR_LEN);
+               }
+#endif /* APSTA_PINGTEST */
+               DHD_EVENT(("MACEVENT: %s, MAC %s\n", event_name, eabuf));
+               break;
+
+       case WLC_E_ASSOC:
+       case WLC_E_REASSOC:
+               if (status == WLC_E_STATUS_SUCCESS) {
+                       DHD_EVENT(("MACEVENT: %s, MAC %s, SUCCESS\n", event_name, eabuf));
+               } else if (status == WLC_E_STATUS_TIMEOUT) {
+                       DHD_EVENT(("MACEVENT: %s, MAC %s, TIMEOUT\n", event_name, eabuf));
+               } else if (status == WLC_E_STATUS_FAIL) {
+                       DHD_EVENT(("MACEVENT: %s, MAC %s, FAILURE, reason %d\n",
+                              event_name, eabuf, (int)reason));
+               } else {
+                       DHD_EVENT(("MACEVENT: %s, MAC %s, unexpected status %d\n",
+                              event_name, eabuf, (int)status));
+               }
+               break;
+
+       case WLC_E_DEAUTH_IND:
+       case WLC_E_DISASSOC_IND:
+#ifdef APSTA_PINGTEST
+               {
+                       int i;
+                       for (i = 0; i < MAX_GUEST; ++i) {
+                               if (bcmp(guest_eas[i].octet, event->addr.octet,
+                                        ETHER_ADDR_LEN) == 0) {
+                                       bzero(guest_eas[i].octet, ETHER_ADDR_LEN);
+                                       break;
+                               }
+                       }
+               }
+#endif /* APSTA_PINGTEST */
+               DHD_EVENT(("MACEVENT: %s, MAC %s, reason %d\n", event_name, eabuf, (int)reason));
+               break;
+
+       case WLC_E_AUTH:
+       case WLC_E_AUTH_IND:
+               if (auth_type == DOT11_OPEN_SYSTEM)
+                       auth_str = "Open System";
+               else if (auth_type == DOT11_SHARED_KEY)
+                       auth_str = "Shared Key";
+               else {
+                       sprintf(err_msg, "AUTH unknown: %d", (int)auth_type);
+                       auth_str = err_msg;
+               }
+               if (event_type == WLC_E_AUTH_IND) {
+                       DHD_EVENT(("MACEVENT: %s, MAC %s, %s\n", event_name, eabuf, auth_str));
+               } else if (status == WLC_E_STATUS_SUCCESS) {
+                       DHD_EVENT(("MACEVENT: %s, MAC %s, %s, SUCCESS\n",
+                               event_name, eabuf, auth_str));
+               } else if (status == WLC_E_STATUS_TIMEOUT) {
+                       DHD_EVENT(("MACEVENT: %s, MAC %s, %s, TIMEOUT\n",
+                               event_name, eabuf, auth_str));
+               } else if (status == WLC_E_STATUS_FAIL) {
+                       DHD_EVENT(("MACEVENT: %s, MAC %s, %s, FAILURE, reason %d\n",
+                              event_name, eabuf, auth_str, (int)reason));
+               }
+
+               break;
+
+       case WLC_E_JOIN:
+       case WLC_E_ROAM:
+       case WLC_E_SET_SSID:
+               if (status == WLC_E_STATUS_SUCCESS) {
+                       DHD_EVENT(("MACEVENT: %s, MAC %s\n", event_name, eabuf));
+               } else if (status == WLC_E_STATUS_FAIL) {
+                       DHD_EVENT(("MACEVENT: %s, failed\n", event_name));
+               } else if (status == WLC_E_STATUS_NO_NETWORKS) {
+                       DHD_EVENT(("MACEVENT: %s, no networks found\n", event_name));
+               } else {
+                       DHD_EVENT(("MACEVENT: %s, unexpected status %d\n",
+                               event_name, (int)status));
+               }
+               break;
+
+       case WLC_E_BEACON_RX:
+               if (status == WLC_E_STATUS_SUCCESS) {
+                       DHD_EVENT(("MACEVENT: %s, SUCCESS\n", event_name));
+               } else if (status == WLC_E_STATUS_FAIL) {
+                       DHD_EVENT(("MACEVENT: %s, FAIL\n", event_name));
+               } else {
+                       DHD_EVENT(("MACEVENT: %s, status %d\n", event_name, status));
+               }
+               break;
+
+       case WLC_E_LINK:
+               DHD_EVENT(("MACEVENT: %s %s\n", event_name, link?"UP":"DOWN"));
+               break;
+
+       case WLC_E_MIC_ERROR:
+               DHD_EVENT(("MACEVENT: %s, MAC %s, Group %d, Flush %d\n",
+                      event_name, eabuf, group, flush_txq));
+               break;
+
+       case WLC_E_ICV_ERROR:
+       case WLC_E_UNICAST_DECODE_ERROR:
+       case WLC_E_MULTICAST_DECODE_ERROR:
+               DHD_EVENT(("MACEVENT: %s, MAC %s\n",
+                      event_name, eabuf));
+               break;
+
+       case WLC_E_TXFAIL:
+               DHD_EVENT(("MACEVENT: %s, RA %s\n", event_name, eabuf));
+               break;
+
+       case WLC_E_SCAN_COMPLETE:
+       case WLC_E_PMKID_CACHE:
+               DHD_EVENT(("MACEVENT: %s\n", event_name));
+               break;
+
+       case WLC_E_PFN_NET_FOUND:
+       case WLC_E_PFN_NET_LOST:
+       case WLC_E_PFN_SCAN_COMPLETE:
+               DHD_EVENT(("PNOEVENT: %s\n", event_name));
+               break;
+
+       case WLC_E_PSK_SUP:
+       case WLC_E_PRUNE:
+               DHD_EVENT(("MACEVENT: %s, status %d, reason %d\n",
+                          event_name, (int)status, (int)reason));
+               break;
+
+       case WLC_E_TRACE:
+               {
+                       static uint32 seqnum_prev = 0;
+                       msgtrace_hdr_t hdr;
+                       uint32 nblost;
+                       char *s, *p;
+
+                       buf = (uchar *) event_data;
+                       memcpy(&hdr, buf, MSGTRACE_HDRLEN);
+
+                       if (hdr.version != MSGTRACE_VERSION) {
+                               printf("\nMACEVENT: %s [unsupported version --> "
+                                      "dhd version:%d dongle version:%d]\n",
+                                      event_name, MSGTRACE_VERSION, hdr.version);
+                               /* Reset datalen to avoid display below */
+                               datalen = 0;
+                               break;
+                       }
+
+                       /* There are 2 bytes available at the end of data */
+                       buf[MSGTRACE_HDRLEN + ntoh16(hdr.len)] = '\0';
+
+                       if (ntoh32(hdr.discarded_bytes) || ntoh32(hdr.discarded_printf)) {
+                               printf("\nWLC_E_TRACE: [Discarded traces in dongle -->"
+                                      "discarded_bytes %d discarded_printf %d]\n",
+                                      ntoh32(hdr.discarded_bytes), ntoh32(hdr.discarded_printf));
+                       }
+
+                       nblost = ntoh32(hdr.seqnum) - seqnum_prev - 1;
+                       if (nblost > 0) {
+                               printf("\nWLC_E_TRACE: [Event lost --> seqnum %d nblost %d\n",
+                                       ntoh32(hdr.seqnum), nblost);
+                       }
+                       seqnum_prev = ntoh32(hdr.seqnum);
+
+                       /* Display the trace buffer. Advance from \n to \n to avoid display big
+                        * printf (issue with Linux printk )
+                        */
+                       p = (char *)&buf[MSGTRACE_HDRLEN];
+                       while ((s = strstr(p, "\n")) != NULL) {
+                               *s = '\0';
+                               printf("%s\n", p);
+                               p = s + 1;
+                       }
+                       printf("%s\n", p);
+
+                       /* Reset datalen to avoid display below */
+                       datalen = 0;
+               }
+               break;
+
+
+       case WLC_E_RSSI:
+               DHD_EVENT(("MACEVENT: %s %d\n", event_name, ntoh32(*((int *)event_data))));
+               break;
+
+       default:
+               DHD_EVENT(("MACEVENT: %s %d, MAC %s, status %d, reason %d, auth %d\n",
+                      event_name, event_type, eabuf, (int)status, (int)reason,
+                      (int)auth_type));
+               break;
+       }
+
+       /* show any appended data */
+       if (datalen) {
+               buf = (uchar *) event_data;
+               DHD_EVENT((" data (%d) : ", datalen));
+               for (i = 0; i < datalen; i++)
+                       DHD_EVENT((" 0x%02x ", *buf++));
+               DHD_EVENT(("\n"));
+       }
+}
+#endif /* SHOW_EVENTS */
+
+int
+wl_host_event(struct dhd_info *dhd, int *ifidx, void *pktdata,
+              wl_event_msg_t *event, void **data_ptr)
+{
+       /* check whether packet is a BRCM event pkt */
+       bcm_event_t *pvt_data = (bcm_event_t *)pktdata;
+       char *event_data;
+       uint32 type, status;
+       uint16 flags;
+       int evlen;
+
+       if (bcmp(BRCM_OUI, &pvt_data->bcm_hdr.oui[0], DOT11_OUI_LEN)) {
+               DHD_ERROR(("%s: mismatched OUI, bailing\n", __FUNCTION__));
+               return (BCME_ERROR);
+       }
+
+       /* BRCM event pkt may be unaligned - use xxx_ua to load user_subtype. */
+       if (ntoh16_ua((void *)&pvt_data->bcm_hdr.usr_subtype) != BCMILCP_BCM_SUBTYPE_EVENT) {
+               DHD_ERROR(("%s: mismatched subtype, bailing\n", __FUNCTION__));
+               return (BCME_ERROR);
+       }
+
+       *data_ptr = &pvt_data[1];
+       event_data = *data_ptr;
+
+       /* memcpy since BRCM event pkt may be unaligned. */
+       memcpy(event, &pvt_data->event, sizeof(wl_event_msg_t));
+
+       type = ntoh32_ua((void *)&event->event_type);
+       flags = ntoh16_ua((void *)&event->flags);
+       status = ntoh32_ua((void *)&event->status);
+       evlen = ntoh32_ua((void *)&event->datalen) + sizeof(bcm_event_t);
+
+       switch (type) {
+               case WLC_E_IF:
+                       {
+                               dhd_if_event_t *ifevent = (dhd_if_event_t *)event_data;
+                               DHD_TRACE(("%s: if event\n", __FUNCTION__));
+
+                               if (ifevent->ifidx > 0 && ifevent->ifidx < DHD_MAX_IFS)
+                               {
+                                       if (ifevent->action == WLC_E_IF_ADD)
+                                               dhd_add_if(dhd, ifevent->ifidx,
+                                                       NULL, event->ifname,
+                                                       pvt_data->eth.ether_dhost,
+                                                       ifevent->flags, ifevent->bssidx);
+                                       else
+                                               dhd_del_if(dhd, ifevent->ifidx);
+                               } else {
+                                       DHD_ERROR(("%s: Invalid ifidx %d for %s\n",
+                                               __FUNCTION__, ifevent->ifidx, event->ifname));
+                               }
+                       }
+                       /* send up the if event: btamp user needs it */
+                       *ifidx = dhd_ifname2idx(dhd, event->ifname);
+                       /* push up to external supp/auth */
+                       dhd_event(dhd, (char *)pvt_data, evlen, *ifidx);
+                       break;
+
+
+#ifdef P2P
+               case WLC_E_NDIS_LINK:
+                       break;
+#endif
+               /* fall through */
+               /* These are what external supplicant/authenticator wants */
+               case WLC_E_LINK:
+               case WLC_E_ASSOC_IND:
+               case WLC_E_REASSOC_IND:
+               case WLC_E_DISASSOC_IND:
+               case WLC_E_MIC_ERROR:
+               default:
+               /* Fall through: this should get _everything_  */
+
+                       *ifidx = dhd_ifname2idx(dhd, event->ifname);
+                       /* push up to external supp/auth */
+                       dhd_event(dhd, (char *)pvt_data, evlen, *ifidx);
+                       DHD_TRACE(("%s: MAC event %d, flags %x, status %x\n",
+                                  __FUNCTION__, type, flags, status));
+
+                       /* put it back to WLC_E_NDIS_LINK */
+                       if (type == WLC_E_NDIS_LINK) {
+                               uint32 temp;
+
+                               temp = ntoh32_ua((void *)&event->event_type);
+                               DHD_TRACE(("Converted to WLC_E_LINK type %d\n", temp));
+
+                               temp = ntoh32(WLC_E_NDIS_LINK);
+                               memcpy((void *)(&pvt_data->event.event_type), &temp,
+                                       sizeof(pvt_data->event.event_type));
+                       }
+                       break;
+       }
+
+#ifdef SHOW_EVENTS
+       wl_show_host_event(event, event_data);
+#endif /* SHOW_EVENTS */
+
+       return (BCME_OK);
+}
+
+
+void
+wl_event_to_host_order(wl_event_msg_t *evt)
+{
+       /* Event struct members passed from dongle to host are stored in network
+        * byte order. Convert all members to host-order.
+        */
+       evt->event_type = ntoh32(evt->event_type);
+       evt->flags = ntoh16(evt->flags);
+       evt->status = ntoh32(evt->status);
+       evt->reason = ntoh32(evt->reason);
+       evt->auth_type = ntoh32(evt->auth_type);
+       evt->datalen = ntoh32(evt->datalen);
+       evt->version = ntoh16(evt->version);
+}