• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Broadcom Dongle Host Driver (DHD), common DHD core.
3  *
4  * Copyright (C) 1999-2009, Broadcom Corporation
5  *
6  *      Unless you and Broadcom execute a separate written software license
7  * agreement governing use of this software, this software is licensed to you
8  * under the terms of the GNU General Public License version 2 (the "GPL"),
9  * available at http://www.broadcom.com/licenses/GPLv2.php, with the
10  * following added to such license:
11  *
12  *      As a special exception, the copyright holders of this software give you
13  * permission to link this software with independent modules, and to copy and
14  * distribute the resulting executable under terms of your choice, provided that
15  * you also meet, for each linked independent module, the terms and conditions of
16  * the license of that module.  An independent module is a module which is not
17  * derived from this software.  The special exception does not apply to any
18  * modifications of the software.
19  *
20  *      Notwithstanding the above, under no circumstances may you combine this
21  * software in any way with any other Broadcom software provided under a license
22  * other than the GPL, without Broadcom's express prior written consent.
23  *
24  * $Id: dhd_common.c,v 1.5.6.8.2.6.6.37 2009/10/22 14:47:18 Exp $
25  */
26 #include <typedefs.h>
27 #include <osl.h>
28 
29 #include <epivers.h>
30 #include <bcmutils.h>
31 
32 #include <bcmendian.h>
33 #include <dngl_stats.h>
34 #include <dhd.h>
35 #include <dhd_bus.h>
36 #include <dhd_proto.h>
37 #include <dhd_dbg.h>
38 #include <msgtrace.h>
39 
40 
41 int dhd_msg_level;
42 
43 char fw_path[MOD_PARAM_PATHLEN];
44 char nv_path[MOD_PARAM_PATHLEN];
45 
46 /* Last connection success/failure status */
47 uint32 dhd_conn_event;
48 uint32 dhd_conn_status;
49 uint32 dhd_conn_reason;
50 
51 #ifdef DHD_DEBUG
52 const char dhd_version[] = "Dongle Host Driver, version " EPI_VERSION_STR "\nCompiled on "
53 	__DATE__ " at " __TIME__;
54 #else
55 const char dhd_version[] = "Dongle Host Driver, version " EPI_VERSION_STR;
56 #endif
57 
58 void dhd_set_timer(void *bus, uint wdtick);
59 
60 
61 /* IOVar table */
62 enum {
63 	IOV_VERSION = 1,
64 	IOV_MSGLEVEL,
65 	IOV_BCMERRORSTR,
66 	IOV_BCMERROR,
67 	IOV_WDTICK,
68 	IOV_DUMP,
69 	IOV_CLEARCOUNTS,
70 	IOV_LOGDUMP,
71 	IOV_LOGCAL,
72 	IOV_LOGSTAMP,
73 	IOV_GPIOOB,
74 	IOV_IOCTLTIMEOUT,
75 	IOV_LAST
76 };
77 
78 const bcm_iovar_t dhd_iovars[] = {
79 	{"version", 	IOV_VERSION,	0,	IOVT_BUFFER,	sizeof(dhd_version) },
80 #ifdef DHD_DEBUG
81 	{"msglevel",	IOV_MSGLEVEL,	0,	IOVT_UINT32,	0 },
82 #endif
83 	{"bcmerrorstr", IOV_BCMERRORSTR, 0, IOVT_BUFFER,	BCME_STRLEN },
84 	{"bcmerror",	IOV_BCMERROR,	0,	IOVT_INT8,	0 },
85 	{"wdtick",	IOV_WDTICK, 0,	IOVT_UINT32,	0 },
86 	{"dump",	IOV_DUMP,	0,	IOVT_BUFFER,	DHD_IOCTL_MAXLEN },
87 	{"clearcounts", IOV_CLEARCOUNTS, 0, IOVT_VOID,	0 },
88 	{"gpioob",	IOV_GPIOOB,	0,	IOVT_UINT32,	0 },
89 	{"ioctl_timeout",	IOV_IOCTLTIMEOUT,	0,	IOVT_UINT32,	0 },
90 	{NULL, 0, 0, 0, 0 }
91 };
92 
93 
94 void
dhd_common_init(void)95 dhd_common_init(void)
96 {
97 	/* Init global variables at run-time, not as part of the declaration.
98 	 * This is required to support init/de-init of the driver. Initialization
99 	 * of globals as part of the declaration results in non-deterministic
100 	 * behaviour since the value of the globals may be different on the
101 	 * first time that the driver is initialized vs subsequent initializations.
102 	 */
103 	dhd_msg_level = DHD_ERROR_VAL;
104 #ifdef CONFIG_BCM4329_FW_PATH
105 	strncpy(fw_path, CONFIG_BCM4329_FW_PATH, MOD_PARAM_PATHLEN-1);
106 #else
107 	fw_path[0] = '\0';
108 #endif
109 #ifdef CONFIG_BCM4329_NVRAM_PATH
110 	strncpy(nv_path, CONFIG_BCM4329_NVRAM_PATH, MOD_PARAM_PATHLEN-1);
111 #else
112 	nv_path[0] = '\0';
113 #endif
114 }
115 
116 static int
dhd_dump(dhd_pub_t * dhdp,char * buf,int buflen)117 dhd_dump(dhd_pub_t *dhdp, char *buf, int buflen)
118 {
119 	char eabuf[ETHER_ADDR_STR_LEN];
120 
121 	struct bcmstrbuf b;
122 	struct bcmstrbuf *strbuf = &b;
123 
124 	bcm_binit(strbuf, buf, buflen);
125 
126 	/* Base DHD info */
127 	bcm_bprintf(strbuf, "%s\n", dhd_version);
128 	bcm_bprintf(strbuf, "\n");
129 	bcm_bprintf(strbuf, "pub.up %d pub.txoff %d pub.busstate %d\n",
130 	            dhdp->up, dhdp->txoff, dhdp->busstate);
131 	bcm_bprintf(strbuf, "pub.hdrlen %d pub.maxctl %d pub.rxsz %d\n",
132 	            dhdp->hdrlen, dhdp->maxctl, dhdp->rxsz);
133 	bcm_bprintf(strbuf, "pub.iswl %d pub.drv_version %ld pub.mac %s\n",
134 	            dhdp->iswl, dhdp->drv_version, bcm_ether_ntoa(&dhdp->mac, eabuf));
135 	bcm_bprintf(strbuf, "pub.bcmerror %d tickcnt %d\n", dhdp->bcmerror, dhdp->tickcnt);
136 
137 	bcm_bprintf(strbuf, "dongle stats:\n");
138 	bcm_bprintf(strbuf, "tx_packets %ld tx_bytes %ld tx_errors %ld tx_dropped %ld\n",
139 	            dhdp->dstats.tx_packets, dhdp->dstats.tx_bytes,
140 	            dhdp->dstats.tx_errors, dhdp->dstats.tx_dropped);
141 	bcm_bprintf(strbuf, "rx_packets %ld rx_bytes %ld rx_errors %ld rx_dropped %ld\n",
142 	            dhdp->dstats.rx_packets, dhdp->dstats.rx_bytes,
143 	            dhdp->dstats.rx_errors, dhdp->dstats.rx_dropped);
144 	bcm_bprintf(strbuf, "multicast %ld\n", dhdp->dstats.multicast);
145 
146 	bcm_bprintf(strbuf, "bus stats:\n");
147 	bcm_bprintf(strbuf, "tx_packets %ld tx_multicast %ld tx_errors %ld\n",
148 	            dhdp->tx_packets, dhdp->tx_multicast, dhdp->tx_errors);
149 	bcm_bprintf(strbuf, "tx_ctlpkts %ld tx_ctlerrs %ld\n",
150 	            dhdp->tx_ctlpkts, dhdp->tx_ctlerrs);
151 	bcm_bprintf(strbuf, "rx_packets %ld rx_multicast %ld rx_errors %ld \n",
152 	            dhdp->rx_packets, dhdp->rx_multicast, dhdp->rx_errors);
153 	bcm_bprintf(strbuf, "rx_ctlpkts %ld rx_ctlerrs %ld rx_dropped %ld rx_flushed %ld\n",
154 	            dhdp->rx_ctlpkts, dhdp->rx_ctlerrs, dhdp->rx_dropped, dhdp->rx_flushed);
155 	bcm_bprintf(strbuf, "rx_readahead_cnt %ld tx_realloc %ld fc_packets %ld\n",
156 	            dhdp->rx_readahead_cnt, dhdp->tx_realloc, dhdp->fc_packets);
157 	bcm_bprintf(strbuf, "wd_dpc_sched %ld\n", dhdp->wd_dpc_sched);
158 	bcm_bprintf(strbuf, "\n");
159 
160 	/* Add any prot info */
161 	dhd_prot_dump(dhdp, strbuf);
162 	bcm_bprintf(strbuf, "\n");
163 
164 	/* Add any bus info */
165 	dhd_bus_dump(dhdp, strbuf);
166 
167 	return (!strbuf->size ? BCME_BUFTOOSHORT : 0);
168 }
169 
170 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)171 dhd_doiovar(dhd_pub_t *dhd_pub, const bcm_iovar_t *vi, uint32 actionid, const char *name,
172             void *params, int plen, void *arg, int len, int val_size)
173 {
174 	int bcmerror = 0;
175 	int32 int_val = 0;
176 
177 	DHD_TRACE(("%s: Enter\n", __FUNCTION__));
178 
179 	if ((bcmerror = bcm_iovar_lencheck(vi, arg, len, IOV_ISSET(actionid))) != 0)
180 		goto exit;
181 
182 	if (plen >= (int)sizeof(int_val))
183 		bcopy(params, &int_val, sizeof(int_val));
184 
185 	switch (actionid) {
186 	case IOV_GVAL(IOV_VERSION):
187 		/* Need to have checked buffer length */
188 		strncpy((char*)arg, dhd_version, len);
189 		break;
190 
191 	case IOV_GVAL(IOV_MSGLEVEL):
192 		int_val = (int32)dhd_msg_level;
193 		bcopy(&int_val, arg, val_size);
194 		break;
195 
196 	case IOV_SVAL(IOV_MSGLEVEL):
197 		dhd_msg_level = int_val;
198 		break;
199 
200 	case IOV_GVAL(IOV_BCMERRORSTR):
201 		strncpy((char *)arg, bcmerrorstr(dhd_pub->bcmerror), BCME_STRLEN);
202 		((char *)arg)[BCME_STRLEN - 1] = 0x00;
203 		break;
204 
205 	case IOV_GVAL(IOV_BCMERROR):
206 		int_val = (int32)dhd_pub->bcmerror;
207 		bcopy(&int_val, arg, val_size);
208 		break;
209 
210 	case IOV_GVAL(IOV_WDTICK):
211 		int_val = (int32)dhd_watchdog_ms;
212 		bcopy(&int_val, arg, val_size);
213 		break;
214 
215 	case IOV_SVAL(IOV_WDTICK):
216 		if (!dhd_pub->up) {
217 			bcmerror = BCME_NOTUP;
218 			break;
219 		}
220 		dhd_os_wd_timer(dhd_pub, (uint)int_val);
221 		break;
222 
223 	case IOV_GVAL(IOV_DUMP):
224 		bcmerror = dhd_dump(dhd_pub, arg, len);
225 		break;
226 
227 
228 	case IOV_SVAL(IOV_CLEARCOUNTS):
229 		dhd_pub->tx_packets = dhd_pub->rx_packets = 0;
230 		dhd_pub->tx_errors = dhd_pub->rx_errors = 0;
231 		dhd_pub->tx_ctlpkts = dhd_pub->rx_ctlpkts = 0;
232 		dhd_pub->tx_ctlerrs = dhd_pub->rx_ctlerrs = 0;
233 		dhd_pub->rx_dropped = 0;
234 		dhd_pub->rx_readahead_cnt = 0;
235 		dhd_pub->tx_realloc = 0;
236 		dhd_pub->wd_dpc_sched = 0;
237 		memset(&dhd_pub->dstats, 0, sizeof(dhd_pub->dstats));
238 		dhd_bus_clearcounts(dhd_pub);
239 		break;
240 
241 
242 	case IOV_GVAL(IOV_IOCTLTIMEOUT): {
243 		int_val = (int32)dhd_os_get_ioctl_resp_timeout();
244 		bcopy(&int_val, arg, sizeof(int_val));
245 		break;
246 	}
247 
248 	case IOV_SVAL(IOV_IOCTLTIMEOUT): {
249 		if (int_val <= 0)
250 			bcmerror = BCME_BADARG;
251 		else
252 			dhd_os_set_ioctl_resp_timeout((unsigned int)int_val);
253 		break;
254 	}
255 
256 
257 	default:
258 		bcmerror = BCME_UNSUPPORTED;
259 		break;
260 	}
261 
262 exit:
263 	return bcmerror;
264 }
265 
266 /* Store the status of a connection attempt for later retrieval by an iovar */
267 void
dhd_store_conn_status(uint32 event,uint32 status,uint32 reason)268 dhd_store_conn_status(uint32 event, uint32 status, uint32 reason)
269 {
270 	/* Do not overwrite a WLC_E_PRUNE with a WLC_E_SET_SSID
271 	 * because an encryption/rsn mismatch results in both events, and
272 	 * the important information is in the WLC_E_PRUNE.
273 	 */
274 	if (!(event == WLC_E_SET_SSID && status == WLC_E_STATUS_FAIL &&
275 	      dhd_conn_event == WLC_E_PRUNE)) {
276 		dhd_conn_event = event;
277 		dhd_conn_status = status;
278 		dhd_conn_reason = reason;
279 	}
280 }
281 
282 bool
dhd_prec_enq(dhd_pub_t * dhdp,struct pktq * q,void * pkt,int prec)283 dhd_prec_enq(dhd_pub_t *dhdp, struct pktq *q, void *pkt, int prec)
284 {
285 	void *p;
286 	int eprec = -1;		/* precedence to evict from */
287 	bool discard_oldest;
288 
289 	/* Fast case, precedence queue is not full and we are also not
290 	 * exceeding total queue length
291 	 */
292 	if (!pktq_pfull(q, prec) && !pktq_full(q)) {
293 		pktq_penq(q, prec, pkt);
294 		return TRUE;
295 	}
296 
297 	/* Determine precedence from which to evict packet, if any */
298 	if (pktq_pfull(q, prec))
299 		eprec = prec;
300 	else if (pktq_full(q)) {
301 		p = pktq_peek_tail(q, &eprec);
302 		ASSERT(p);
303 		if (eprec > prec)
304 			return FALSE;
305 	}
306 
307 	/* Evict if needed */
308 	if (eprec >= 0) {
309 		/* Detect queueing to unconfigured precedence */
310 		ASSERT(!pktq_pempty(q, eprec));
311 		discard_oldest = AC_BITMAP_TST(dhdp->wme_dp, eprec);
312 		if (eprec == prec && !discard_oldest)
313 			return FALSE;		/* refuse newer (incoming) packet */
314 		/* Evict packet according to discard policy */
315 		p = discard_oldest ? pktq_pdeq(q, eprec) : pktq_pdeq_tail(q, eprec);
316 		if (p == NULL) {
317 			DHD_ERROR(("%s: pktq_penq() failed, oldest %d.",
318 				__FUNCTION__, discard_oldest));
319 			ASSERT(p);
320 		}
321 
322 		PKTFREE(dhdp->osh, p, TRUE);
323 	}
324 
325 	/* Enqueue */
326 	p = pktq_penq(q, prec, pkt);
327 	if (p == NULL) {
328 		DHD_ERROR(("%s: pktq_penq() failed.", __FUNCTION__));
329 		ASSERT(p);
330 	}
331 
332 	return TRUE;
333 }
334 
335 static int
dhd_iovar_op(dhd_pub_t * dhd_pub,const char * name,void * params,int plen,void * arg,int len,bool set)336 dhd_iovar_op(dhd_pub_t *dhd_pub, const char *name,
337              void *params, int plen, void *arg, int len, bool set)
338 {
339 	int bcmerror = 0;
340 	int val_size;
341 	const bcm_iovar_t *vi = NULL;
342 	uint32 actionid;
343 
344 	DHD_TRACE(("%s: Enter\n", __FUNCTION__));
345 
346 	ASSERT(name);
347 	ASSERT(len >= 0);
348 
349 	/* Get MUST have return space */
350 	ASSERT(set || (arg && len));
351 
352 	/* Set does NOT take qualifiers */
353 	ASSERT(!set || (!params && !plen));
354 
355 	if ((vi = bcm_iovar_lookup(dhd_iovars, name)) == NULL) {
356 		bcmerror = BCME_UNSUPPORTED;
357 		goto exit;
358 	}
359 
360 	DHD_CTL(("%s: %s %s, len %d plen %d\n", __FUNCTION__,
361 	         name, (set ? "set" : "get"), len, plen));
362 
363 	/* set up 'params' pointer in case this is a set command so that
364 	 * the convenience int and bool code can be common to set and get
365 	 */
366 	if (params == NULL) {
367 		params = arg;
368 		plen = len;
369 	}
370 
371 	if (vi->type == IOVT_VOID)
372 		val_size = 0;
373 	else if (vi->type == IOVT_BUFFER)
374 		val_size = len;
375 	else
376 		/* all other types are integer sized */
377 		val_size = sizeof(int);
378 
379 	actionid = set ? IOV_SVAL(vi->varid) : IOV_GVAL(vi->varid);
380 	bcmerror = dhd_doiovar(dhd_pub, vi, actionid, name, params, plen, arg, len, val_size);
381 
382 exit:
383 	return bcmerror;
384 }
385 
386 int
dhd_ioctl(dhd_pub_t * dhd_pub,dhd_ioctl_t * ioc,void * buf,uint buflen)387 dhd_ioctl(dhd_pub_t *dhd_pub, dhd_ioctl_t *ioc, void *buf, uint buflen)
388 {
389 	int bcmerror = 0;
390 
391 	DHD_TRACE(("%s: Enter\n", __FUNCTION__));
392 
393 	if (!buf) return BCME_BADARG;
394 
395 	switch (ioc->cmd) {
396 	case DHD_GET_MAGIC:
397 		if (buflen < sizeof(int))
398 			bcmerror = BCME_BUFTOOSHORT;
399 		else
400 			*(int*)buf = DHD_IOCTL_MAGIC;
401 		break;
402 
403 	case DHD_GET_VERSION:
404 		if (buflen < sizeof(int))
405 			bcmerror = -BCME_BUFTOOSHORT;
406 		else
407 			*(int*)buf = DHD_IOCTL_VERSION;
408 		break;
409 
410 	case DHD_GET_VAR:
411 	case DHD_SET_VAR: {
412 		char *arg;
413 		uint arglen;
414 
415 		/* scan past the name to any arguments */
416 		for (arg = buf, arglen = buflen; *arg && arglen; arg++, arglen--);
417 
418 		if (*arg) {
419 			bcmerror = BCME_BUFTOOSHORT;
420 			break;
421 		}
422 
423 		/* account for the NUL terminator */
424 		arg++, arglen--;
425 
426 		/* call with the appropriate arguments */
427 		if (ioc->cmd == DHD_GET_VAR)
428 			bcmerror = dhd_iovar_op(dhd_pub, buf, arg, arglen,
429 			buf, buflen, IOV_GET);
430 		else
431 			bcmerror = dhd_iovar_op(dhd_pub, buf, NULL, 0, arg, arglen, IOV_SET);
432 		if (bcmerror != BCME_UNSUPPORTED)
433 			break;
434 
435 		/* not in generic table, try protocol module */
436 		if (ioc->cmd == DHD_GET_VAR)
437 			bcmerror = dhd_prot_iovar_op(dhd_pub, buf, arg,
438 			                             arglen, buf, buflen, IOV_GET);
439 		else
440 			bcmerror = dhd_prot_iovar_op(dhd_pub, buf,
441 			                             NULL, 0, arg, arglen, IOV_SET);
442 		if (bcmerror != BCME_UNSUPPORTED)
443 			break;
444 
445 		/* if still not found, try bus module */
446 		if (ioc->cmd == DHD_GET_VAR)
447 			bcmerror = dhd_bus_iovar_op(dhd_pub, buf,
448 			                            arg, arglen, buf, buflen, IOV_GET);
449 		else
450 			bcmerror = dhd_bus_iovar_op(dhd_pub, buf,
451 			                            NULL, 0, arg, arglen, IOV_SET);
452 
453 		break;
454 	}
455 
456 	default:
457 		bcmerror = BCME_UNSUPPORTED;
458 	}
459 
460 	return bcmerror;
461 }
462 
463 #ifdef APSTA_PINGTEST
464 struct ether_addr guest_eas[MAX_GUEST];
465 #endif
466 
467 #ifdef SHOW_EVENTS
468 static void
wl_show_host_event(wl_event_msg_t * event,void * event_data)469 wl_show_host_event(wl_event_msg_t *event, void *event_data)
470 {
471 	uint i, status, reason;
472 	bool group = FALSE, flush_txq = FALSE, link = FALSE;
473 	char *auth_str, *event_name;
474 	uchar *buf;
475 	char err_msg[256], eabuf[ETHER_ADDR_STR_LEN];
476 	static struct {uint event; char *event_name;} event_names[] = {
477 		{WLC_E_SET_SSID, "SET_SSID"},
478 		{WLC_E_JOIN, "JOIN"},
479 		{WLC_E_START, "START"},
480 		{WLC_E_AUTH, "AUTH"},
481 		{WLC_E_AUTH_IND, "AUTH_IND"},
482 		{WLC_E_DEAUTH, "DEAUTH"},
483 		{WLC_E_DEAUTH_IND, "DEAUTH_IND"},
484 		{WLC_E_ASSOC, "ASSOC"},
485 		{WLC_E_ASSOC_IND, "ASSOC_IND"},
486 		{WLC_E_REASSOC, "REASSOC"},
487 		{WLC_E_REASSOC_IND, "REASSOC_IND"},
488 		{WLC_E_DISASSOC, "DISASSOC"},
489 		{WLC_E_DISASSOC_IND, "DISASSOC_IND"},
490 		{WLC_E_QUIET_START, "START_QUIET"},
491 		{WLC_E_QUIET_END, "END_QUIET"},
492 		{WLC_E_BEACON_RX, "BEACON_RX"},
493 		{WLC_E_LINK, "LINK"},
494 		{WLC_E_MIC_ERROR, "MIC_ERROR"},
495 		{WLC_E_NDIS_LINK, "NDIS_LINK"},
496 		{WLC_E_ROAM, "ROAM"},
497 		{WLC_E_TXFAIL, "TXFAIL"},
498 		{WLC_E_PMKID_CACHE, "PMKID_CACHE"},
499 		{WLC_E_RETROGRADE_TSF, "RETROGRADE_TSF"},
500 		{WLC_E_PRUNE, "PRUNE"},
501 		{WLC_E_AUTOAUTH, "AUTOAUTH"},
502 		{WLC_E_EAPOL_MSG, "EAPOL_MSG"},
503 		{WLC_E_SCAN_COMPLETE, "SCAN_COMPLETE"},
504 		{WLC_E_ADDTS_IND, "ADDTS_IND"},
505 		{WLC_E_DELTS_IND, "DELTS_IND"},
506 		{WLC_E_BCNSENT_IND, "BCNSENT_IND"},
507 		{WLC_E_BCNRX_MSG, "BCNRX_MSG"},
508 		{WLC_E_BCNLOST_MSG, "BCNLOST_MSG"},
509 		{WLC_E_ROAM_PREP, "ROAM_PREP"},
510 		{WLC_E_PFN_NET_FOUND, "PNO_NET_FOUND"},
511 		{WLC_E_PFN_NET_LOST, "PNO_NET_LOST"},
512 		{WLC_E_RESET_COMPLETE, "RESET_COMPLETE"},
513 		{WLC_E_JOIN_START, "JOIN_START"},
514 		{WLC_E_ROAM_START, "ROAM_START"},
515 		{WLC_E_ASSOC_START, "ASSOC_START"},
516 		{WLC_E_IBSS_ASSOC, "IBSS_ASSOC"},
517 		{WLC_E_RADIO, "RADIO"},
518 		{WLC_E_PSM_WATCHDOG, "PSM_WATCHDOG"},
519 		{WLC_E_PROBREQ_MSG, "PROBREQ_MSG"},
520 		{WLC_E_SCAN_CONFIRM_IND, "SCAN_CONFIRM_IND"},
521 		{WLC_E_PSK_SUP, "PSK_SUP"},
522 		{WLC_E_COUNTRY_CODE_CHANGED, "COUNTRY_CODE_CHANGED"},
523 		{WLC_E_EXCEEDED_MEDIUM_TIME, "EXCEEDED_MEDIUM_TIME"},
524 		{WLC_E_ICV_ERROR, "ICV_ERROR"},
525 		{WLC_E_UNICAST_DECODE_ERROR, "UNICAST_DECODE_ERROR"},
526 		{WLC_E_MULTICAST_DECODE_ERROR, "MULTICAST_DECODE_ERROR"},
527 		{WLC_E_TRACE, "TRACE"},
528 		{WLC_E_ACTION_FRAME, "ACTION FRAME"},
529 		{WLC_E_ACTION_FRAME_COMPLETE, "ACTION FRAME TX COMPLETE"},
530 		{WLC_E_IF, "IF"},
531 		{WLC_E_RSSI, "RSSI"},
532 		{WLC_E_PFN_SCAN_COMPLETE, "SCAN_COMPLETE"}
533 	};
534 	uint event_type, flags, auth_type, datalen;
535 	event_type = ntoh32(event->event_type);
536 	flags = ntoh16(event->flags);
537 	status = ntoh32(event->status);
538 	reason = ntoh32(event->reason);
539 	auth_type = ntoh32(event->auth_type);
540 	datalen = ntoh32(event->datalen);
541 	/* debug dump of event messages */
542 	sprintf(eabuf, "%02x:%02x:%02x:%02x:%02x:%02x",
543 	        (uchar)event->addr.octet[0]&0xff,
544 	        (uchar)event->addr.octet[1]&0xff,
545 	        (uchar)event->addr.octet[2]&0xff,
546 	        (uchar)event->addr.octet[3]&0xff,
547 	        (uchar)event->addr.octet[4]&0xff,
548 	        (uchar)event->addr.octet[5]&0xff);
549 
550 	event_name = "UNKNOWN";
551 	for (i = 0; i < ARRAYSIZE(event_names); i++) {
552 		if (event_names[i].event == event_type)
553 			event_name = event_names[i].event_name;
554 	}
555 
556 	if (flags & WLC_EVENT_MSG_LINK)
557 		link = TRUE;
558 	if (flags & WLC_EVENT_MSG_GROUP)
559 		group = TRUE;
560 	if (flags & WLC_EVENT_MSG_FLUSHTXQ)
561 		flush_txq = TRUE;
562 
563 	switch (event_type) {
564 	case WLC_E_START:
565 	case WLC_E_DEAUTH:
566 	case WLC_E_DISASSOC:
567 		DHD_EVENT(("MACEVENT: %s, MAC %s\n", event_name, eabuf));
568 		break;
569 
570 	case WLC_E_ASSOC_IND:
571 	case WLC_E_REASSOC_IND:
572 #ifdef APSTA_PINGTEST
573 		{
574 			int i;
575 			for (i = 0; i < MAX_GUEST; ++i)
576 				if (ETHER_ISNULLADDR(&guest_eas[i]))
577 					break;
578 			if (i < MAX_GUEST)
579 				bcopy(event->addr.octet, guest_eas[i].octet, ETHER_ADDR_LEN);
580 		}
581 #endif /* APSTA_PINGTEST */
582 		DHD_EVENT(("MACEVENT: %s, MAC %s\n", event_name, eabuf));
583 		break;
584 
585 	case WLC_E_ASSOC:
586 	case WLC_E_REASSOC:
587 		if (status == WLC_E_STATUS_SUCCESS) {
588 			DHD_EVENT(("MACEVENT: %s, MAC %s, SUCCESS\n", event_name, eabuf));
589 		} else if (status == WLC_E_STATUS_TIMEOUT) {
590 			DHD_EVENT(("MACEVENT: %s, MAC %s, TIMEOUT\n", event_name, eabuf));
591 		} else if (status == WLC_E_STATUS_FAIL) {
592 			DHD_EVENT(("MACEVENT: %s, MAC %s, FAILURE, reason %d\n",
593 			       event_name, eabuf, (int)reason));
594 		} else {
595 			DHD_EVENT(("MACEVENT: %s, MAC %s, unexpected status %d\n",
596 			       event_name, eabuf, (int)status));
597 		}
598 		break;
599 
600 	case WLC_E_DEAUTH_IND:
601 	case WLC_E_DISASSOC_IND:
602 #ifdef APSTA_PINGTEST
603 		{
604 			int i;
605 			for (i = 0; i < MAX_GUEST; ++i) {
606 				if (bcmp(guest_eas[i].octet, event->addr.octet,
607 				         ETHER_ADDR_LEN) == 0) {
608 					bzero(guest_eas[i].octet, ETHER_ADDR_LEN);
609 					break;
610 				}
611 			}
612 		}
613 #endif /* APSTA_PINGTEST */
614 		DHD_EVENT(("MACEVENT: %s, MAC %s, reason %d\n", event_name, eabuf, (int)reason));
615 		break;
616 
617 	case WLC_E_AUTH:
618 	case WLC_E_AUTH_IND:
619 		if (auth_type == DOT11_OPEN_SYSTEM)
620 			auth_str = "Open System";
621 		else if (auth_type == DOT11_SHARED_KEY)
622 			auth_str = "Shared Key";
623 		else {
624 			sprintf(err_msg, "AUTH unknown: %d", (int)auth_type);
625 			auth_str = err_msg;
626 		}
627 		if (event_type == WLC_E_AUTH_IND) {
628 			DHD_EVENT(("MACEVENT: %s, MAC %s, %s\n", event_name, eabuf, auth_str));
629 		} else if (status == WLC_E_STATUS_SUCCESS) {
630 			DHD_EVENT(("MACEVENT: %s, MAC %s, %s, SUCCESS\n",
631 				event_name, eabuf, auth_str));
632 		} else if (status == WLC_E_STATUS_TIMEOUT) {
633 			DHD_EVENT(("MACEVENT: %s, MAC %s, %s, TIMEOUT\n",
634 				event_name, eabuf, auth_str));
635 		} else if (status == WLC_E_STATUS_FAIL) {
636 			DHD_EVENT(("MACEVENT: %s, MAC %s, %s, FAILURE, reason %d\n",
637 			       event_name, eabuf, auth_str, (int)reason));
638 		}
639 
640 		break;
641 
642 	case WLC_E_JOIN:
643 	case WLC_E_ROAM:
644 	case WLC_E_SET_SSID:
645 		if (status == WLC_E_STATUS_SUCCESS) {
646 			DHD_EVENT(("MACEVENT: %s, MAC %s\n", event_name, eabuf));
647 		} else if (status == WLC_E_STATUS_FAIL) {
648 			DHD_EVENT(("MACEVENT: %s, failed\n", event_name));
649 		} else if (status == WLC_E_STATUS_NO_NETWORKS) {
650 			DHD_EVENT(("MACEVENT: %s, no networks found\n", event_name));
651 		} else {
652 			DHD_EVENT(("MACEVENT: %s, unexpected status %d\n",
653 				event_name, (int)status));
654 		}
655 		break;
656 
657 	case WLC_E_BEACON_RX:
658 		if (status == WLC_E_STATUS_SUCCESS) {
659 			DHD_EVENT(("MACEVENT: %s, SUCCESS\n", event_name));
660 		} else if (status == WLC_E_STATUS_FAIL) {
661 			DHD_EVENT(("MACEVENT: %s, FAIL\n", event_name));
662 		} else {
663 			DHD_EVENT(("MACEVENT: %s, status %d\n", event_name, status));
664 		}
665 		break;
666 
667 	case WLC_E_LINK:
668 		DHD_EVENT(("MACEVENT: %s %s\n", event_name, link?"UP":"DOWN"));
669 		break;
670 
671 	case WLC_E_MIC_ERROR:
672 		DHD_EVENT(("MACEVENT: %s, MAC %s, Group %d, Flush %d\n",
673 		       event_name, eabuf, group, flush_txq));
674 		break;
675 
676 	case WLC_E_ICV_ERROR:
677 	case WLC_E_UNICAST_DECODE_ERROR:
678 	case WLC_E_MULTICAST_DECODE_ERROR:
679 		DHD_EVENT(("MACEVENT: %s, MAC %s\n",
680 		       event_name, eabuf));
681 		break;
682 
683 	case WLC_E_TXFAIL:
684 		DHD_EVENT(("MACEVENT: %s, RA %s\n", event_name, eabuf));
685 		break;
686 
687 	case WLC_E_SCAN_COMPLETE:
688 	case WLC_E_PMKID_CACHE:
689 		DHD_EVENT(("MACEVENT: %s\n", event_name));
690 		break;
691 
692 	case WLC_E_PFN_NET_FOUND:
693 	case WLC_E_PFN_NET_LOST:
694 	case WLC_E_PFN_SCAN_COMPLETE:
695 		DHD_EVENT(("PNOEVENT: %s\n", event_name));
696 		break;
697 
698 	case WLC_E_PSK_SUP:
699 	case WLC_E_PRUNE:
700 		DHD_EVENT(("MACEVENT: %s, status %d, reason %d\n",
701 		           event_name, (int)status, (int)reason));
702 		break;
703 
704 	case WLC_E_TRACE:
705 		{
706 			static uint32 seqnum_prev = 0;
707 			msgtrace_hdr_t hdr;
708 			uint32 nblost;
709 			char *s, *p;
710 
711 			buf = (uchar *) event_data;
712 			memcpy(&hdr, buf, MSGTRACE_HDRLEN);
713 
714 			if (hdr.version != MSGTRACE_VERSION) {
715 				printf("\nMACEVENT: %s [unsupported version --> "
716 				       "dhd version:%d dongle version:%d]\n",
717 				       event_name, MSGTRACE_VERSION, hdr.version);
718 				/* Reset datalen to avoid display below */
719 				datalen = 0;
720 				break;
721 			}
722 
723 			/* There are 2 bytes available at the end of data */
724 			buf[MSGTRACE_HDRLEN + ntoh16(hdr.len)] = '\0';
725 
726 			if (ntoh32(hdr.discarded_bytes) || ntoh32(hdr.discarded_printf)) {
727 				printf("\nWLC_E_TRACE: [Discarded traces in dongle -->"
728 				       "discarded_bytes %d discarded_printf %d]\n",
729 				       ntoh32(hdr.discarded_bytes), ntoh32(hdr.discarded_printf));
730 			}
731 
732 			nblost = ntoh32(hdr.seqnum) - seqnum_prev - 1;
733 			if (nblost > 0) {
734 				printf("\nWLC_E_TRACE: [Event lost --> seqnum %d nblost %d\n",
735 				        ntoh32(hdr.seqnum), nblost);
736 			}
737 			seqnum_prev = ntoh32(hdr.seqnum);
738 
739 			/* Display the trace buffer. Advance from \n to \n to avoid display big
740 			 * printf (issue with Linux printk )
741 			 */
742 			p = (char *)&buf[MSGTRACE_HDRLEN];
743 			while ((s = strstr(p, "\n")) != NULL) {
744 				*s = '\0';
745 				printf("%s\n", p);
746 				p = s + 1;
747 			}
748 			printf("%s\n", p);
749 
750 			/* Reset datalen to avoid display below */
751 			datalen = 0;
752 		}
753 		break;
754 
755 
756 	case WLC_E_RSSI:
757 		DHD_EVENT(("MACEVENT: %s %d\n", event_name, ntoh32(*((int *)event_data))));
758 		break;
759 
760 	default:
761 		DHD_EVENT(("MACEVENT: %s %d, MAC %s, status %d, reason %d, auth %d\n",
762 		       event_name, event_type, eabuf, (int)status, (int)reason,
763 		       (int)auth_type));
764 		break;
765 	}
766 
767 	/* show any appended data */
768 	if (datalen) {
769 		buf = (uchar *) event_data;
770 		DHD_EVENT((" data (%d) : ", datalen));
771 		for (i = 0; i < datalen; i++)
772 			DHD_EVENT((" 0x%02x ", *buf++));
773 		DHD_EVENT(("\n"));
774 	}
775 }
776 #endif /* SHOW_EVENTS */
777 
778 int
wl_host_event(struct dhd_info * dhd,int * ifidx,void * pktdata,wl_event_msg_t * event,void ** data_ptr)779 wl_host_event(struct dhd_info *dhd, int *ifidx, void *pktdata,
780               wl_event_msg_t *event, void **data_ptr)
781 {
782 	/* check whether packet is a BRCM event pkt */
783 	bcm_event_t *pvt_data = (bcm_event_t *)pktdata;
784 	char *event_data;
785 	uint32 type, status;
786 	uint16 flags;
787 	int evlen;
788 
789 	if (bcmp(BRCM_OUI, &pvt_data->bcm_hdr.oui[0], DOT11_OUI_LEN)) {
790 		DHD_ERROR(("%s: mismatched OUI, bailing\n", __FUNCTION__));
791 		return (BCME_ERROR);
792 	}
793 
794 	/* BRCM event pkt may be unaligned - use xxx_ua to load user_subtype. */
795 	if (ntoh16_ua((void *)&pvt_data->bcm_hdr.usr_subtype) != BCMILCP_BCM_SUBTYPE_EVENT) {
796 		DHD_ERROR(("%s: mismatched subtype, bailing\n", __FUNCTION__));
797 		return (BCME_ERROR);
798 	}
799 
800 	*data_ptr = &pvt_data[1];
801 	event_data = *data_ptr;
802 
803 	/* memcpy since BRCM event pkt may be unaligned. */
804 	memcpy(event, &pvt_data->event, sizeof(wl_event_msg_t));
805 
806 	type = ntoh32_ua((void *)&event->event_type);
807 	flags = ntoh16_ua((void *)&event->flags);
808 	status = ntoh32_ua((void *)&event->status);
809 	evlen = ntoh32_ua((void *)&event->datalen) + sizeof(bcm_event_t);
810 
811 	switch (type) {
812 		case WLC_E_IF:
813 			{
814 				dhd_if_event_t *ifevent = (dhd_if_event_t *)event_data;
815 				DHD_TRACE(("%s: if event\n", __FUNCTION__));
816 
817 				if (ifevent->ifidx > 0 && ifevent->ifidx < DHD_MAX_IFS)
818 				{
819 					if (ifevent->action == WLC_E_IF_ADD)
820 						dhd_add_if(dhd, ifevent->ifidx,
821 							NULL, event->ifname,
822 							pvt_data->eth.ether_dhost,
823 							ifevent->flags, ifevent->bssidx);
824 					else
825 						dhd_del_if(dhd, ifevent->ifidx);
826 				} else {
827 					DHD_ERROR(("%s: Invalid ifidx %d for %s\n",
828 						__FUNCTION__, ifevent->ifidx, event->ifname));
829 				}
830 			}
831 			/* send up the if event: btamp user needs it */
832 			*ifidx = dhd_ifname2idx(dhd, event->ifname);
833 			/* push up to external supp/auth */
834 			dhd_event(dhd, (char *)pvt_data, evlen, *ifidx);
835 			break;
836 
837 
838 #ifdef P2P
839 		case WLC_E_NDIS_LINK:
840 			break;
841 #endif
842 		/* fall through */
843 		/* These are what external supplicant/authenticator wants */
844 		case WLC_E_LINK:
845 		case WLC_E_ASSOC_IND:
846 		case WLC_E_REASSOC_IND:
847 		case WLC_E_DISASSOC_IND:
848 		case WLC_E_MIC_ERROR:
849 		default:
850 		/* Fall through: this should get _everything_  */
851 
852 			*ifidx = dhd_ifname2idx(dhd, event->ifname);
853 			/* push up to external supp/auth */
854 			dhd_event(dhd, (char *)pvt_data, evlen, *ifidx);
855 			DHD_TRACE(("%s: MAC event %d, flags %x, status %x\n",
856 			           __FUNCTION__, type, flags, status));
857 
858 			/* put it back to WLC_E_NDIS_LINK */
859 			if (type == WLC_E_NDIS_LINK) {
860 				uint32 temp;
861 
862 				temp = ntoh32_ua((void *)&event->event_type);
863 				DHD_TRACE(("Converted to WLC_E_LINK type %d\n", temp));
864 
865 				temp = ntoh32(WLC_E_NDIS_LINK);
866 				memcpy((void *)(&pvt_data->event.event_type), &temp,
867 					sizeof(pvt_data->event.event_type));
868 			}
869 			break;
870 	}
871 
872 #ifdef SHOW_EVENTS
873 	wl_show_host_event(event, event_data);
874 #endif /* SHOW_EVENTS */
875 
876 	return (BCME_OK);
877 }
878 
879 
880 void
wl_event_to_host_order(wl_event_msg_t * evt)881 wl_event_to_host_order(wl_event_msg_t *evt)
882 {
883 	/* Event struct members passed from dongle to host are stored in network
884 	 * byte order. Convert all members to host-order.
885 	 */
886 	evt->event_type = ntoh32(evt->event_type);
887 	evt->flags = ntoh16(evt->flags);
888 	evt->status = ntoh32(evt->status);
889 	evt->reason = ntoh32(evt->reason);
890 	evt->auth_type = ntoh32(evt->auth_type);
891 	evt->datalen = ntoh32(evt->datalen);
892 	evt->version = ntoh16(evt->version);
893 }
894