spi-topcliff-pch: supports a spi mode setup and bit order setup by IO control
[zen-stable.git] / drivers / net / wireless / brcm80211 / brcmfmac / dhd_common.c
bloba51d8f5d36fcf843bf311e5e257d560bb134e134
1 /*
2 * Copyright (c) 2010 Broadcom Corporation
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.
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.
16 #include <linux/kernel.h>
17 #include <linux/string.h>
18 #include <linux/sched.h>
19 #include <linux/netdevice.h>
20 #include <asm/unaligned.h>
21 #include <defs.h>
22 #include <brcmu_wifi.h>
23 #include <brcmu_utils.h>
24 #include "dhd.h"
25 #include "dhd_bus.h"
26 #include "dhd_proto.h"
27 #include "dhd_dbg.h"
29 #define BRCM_OUI "\x00\x10\x18"
30 #define DOT11_OUI_LEN 3
31 #define BCMILCP_BCM_SUBTYPE_EVENT 1
32 #define PKTFILTER_BUF_SIZE 2048
33 #define BRCMF_ARPOL_MODE 0xb /* agent|snoop|peer_autoreply */
35 #define MSGTRACE_VERSION 1
37 #define BRCMF_PKT_FILTER_FIXED_LEN offsetof(struct brcmf_pkt_filter_le, u)
38 #define BRCMF_PKT_FILTER_PATTERN_FIXED_LEN \
39 offsetof(struct brcmf_pkt_filter_pattern_le, mask_and_pattern)
41 #ifdef BCMDBG
42 static const char brcmf_version[] =
43 "Dongle Host Driver, version " BRCMF_VERSION_STR "\nCompiled on "
44 __DATE__ " at " __TIME__;
45 #else
46 static const char brcmf_version[] =
47 "Dongle Host Driver, version " BRCMF_VERSION_STR;
48 #endif
50 /* Message trace header */
51 struct msgtrace_hdr {
52 u8 version;
53 u8 spare;
54 __be16 len; /* Len of the trace */
55 __be32 seqnum; /* Sequence number of message. Useful
56 * if the messsage has been lost
57 * because of DMA error or a bus reset
58 * (ex: SDIO Func2)
60 __be32 discarded_bytes; /* Number of discarded bytes because of
61 trace overflow */
62 __be32 discarded_printf; /* Number of discarded printf
63 because of trace overflow */
64 } __packed;
67 uint
68 brcmf_c_mkiovar(char *name, char *data, uint datalen, char *buf, uint buflen)
70 uint len;
72 len = strlen(name) + 1;
74 if ((len + datalen) > buflen)
75 return 0;
77 strncpy(buf, name, buflen);
79 /* append data onto the end of the name string */
80 memcpy(&buf[len], data, datalen);
81 len += datalen;
83 return len;
86 bool brcmf_c_prec_enq(struct device *dev, struct pktq *q,
87 struct sk_buff *pkt, int prec)
89 struct sk_buff *p;
90 int eprec = -1; /* precedence to evict from */
91 bool discard_oldest;
92 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
93 struct brcmf_pub *drvr = bus_if->drvr;
95 /* Fast case, precedence queue is not full and we are also not
96 * exceeding total queue length
98 if (!pktq_pfull(q, prec) && !pktq_full(q)) {
99 brcmu_pktq_penq(q, prec, pkt);
100 return true;
103 /* Determine precedence from which to evict packet, if any */
104 if (pktq_pfull(q, prec))
105 eprec = prec;
106 else if (pktq_full(q)) {
107 p = brcmu_pktq_peek_tail(q, &eprec);
108 if (eprec > prec)
109 return false;
112 /* Evict if needed */
113 if (eprec >= 0) {
114 /* Detect queueing to unconfigured precedence */
115 discard_oldest = ac_bitmap_tst(drvr->wme_dp, eprec);
116 if (eprec == prec && !discard_oldest)
117 return false; /* refuse newer (incoming) packet */
118 /* Evict packet according to discard policy */
119 p = discard_oldest ? brcmu_pktq_pdeq(q, eprec) :
120 brcmu_pktq_pdeq_tail(q, eprec);
121 if (p == NULL)
122 brcmf_dbg(ERROR, "brcmu_pktq_penq() failed, oldest %d\n",
123 discard_oldest);
125 brcmu_pkt_buf_free_skb(p);
128 /* Enqueue */
129 p = brcmu_pktq_penq(q, prec, pkt);
130 if (p == NULL)
131 brcmf_dbg(ERROR, "brcmu_pktq_penq() failed\n");
133 return p != NULL;
136 #ifdef BCMDBG
137 static void
138 brcmf_c_show_host_event(struct brcmf_event_msg *event, void *event_data)
140 uint i, status, reason;
141 bool group = false, flush_txq = false, link = false;
142 char *auth_str, *event_name;
143 unsigned char *buf;
144 char err_msg[256], eabuf[ETHER_ADDR_STR_LEN];
145 static struct {
146 uint event;
147 char *event_name;
148 } event_names[] = {
150 BRCMF_E_SET_SSID, "SET_SSID"}, {
151 BRCMF_E_JOIN, "JOIN"}, {
152 BRCMF_E_START, "START"}, {
153 BRCMF_E_AUTH, "AUTH"}, {
154 BRCMF_E_AUTH_IND, "AUTH_IND"}, {
155 BRCMF_E_DEAUTH, "DEAUTH"}, {
156 BRCMF_E_DEAUTH_IND, "DEAUTH_IND"}, {
157 BRCMF_E_ASSOC, "ASSOC"}, {
158 BRCMF_E_ASSOC_IND, "ASSOC_IND"}, {
159 BRCMF_E_REASSOC, "REASSOC"}, {
160 BRCMF_E_REASSOC_IND, "REASSOC_IND"}, {
161 BRCMF_E_DISASSOC, "DISASSOC"}, {
162 BRCMF_E_DISASSOC_IND, "DISASSOC_IND"}, {
163 BRCMF_E_QUIET_START, "START_QUIET"}, {
164 BRCMF_E_QUIET_END, "END_QUIET"}, {
165 BRCMF_E_BEACON_RX, "BEACON_RX"}, {
166 BRCMF_E_LINK, "LINK"}, {
167 BRCMF_E_MIC_ERROR, "MIC_ERROR"}, {
168 BRCMF_E_NDIS_LINK, "NDIS_LINK"}, {
169 BRCMF_E_ROAM, "ROAM"}, {
170 BRCMF_E_TXFAIL, "TXFAIL"}, {
171 BRCMF_E_PMKID_CACHE, "PMKID_CACHE"}, {
172 BRCMF_E_RETROGRADE_TSF, "RETROGRADE_TSF"}, {
173 BRCMF_E_PRUNE, "PRUNE"}, {
174 BRCMF_E_AUTOAUTH, "AUTOAUTH"}, {
175 BRCMF_E_EAPOL_MSG, "EAPOL_MSG"}, {
176 BRCMF_E_SCAN_COMPLETE, "SCAN_COMPLETE"}, {
177 BRCMF_E_ADDTS_IND, "ADDTS_IND"}, {
178 BRCMF_E_DELTS_IND, "DELTS_IND"}, {
179 BRCMF_E_BCNSENT_IND, "BCNSENT_IND"}, {
180 BRCMF_E_BCNRX_MSG, "BCNRX_MSG"}, {
181 BRCMF_E_BCNLOST_MSG, "BCNLOST_MSG"}, {
182 BRCMF_E_ROAM_PREP, "ROAM_PREP"}, {
183 BRCMF_E_PFN_NET_FOUND, "PNO_NET_FOUND"}, {
184 BRCMF_E_PFN_NET_LOST, "PNO_NET_LOST"}, {
185 BRCMF_E_RESET_COMPLETE, "RESET_COMPLETE"}, {
186 BRCMF_E_JOIN_START, "JOIN_START"}, {
187 BRCMF_E_ROAM_START, "ROAM_START"}, {
188 BRCMF_E_ASSOC_START, "ASSOC_START"}, {
189 BRCMF_E_IBSS_ASSOC, "IBSS_ASSOC"}, {
190 BRCMF_E_RADIO, "RADIO"}, {
191 BRCMF_E_PSM_WATCHDOG, "PSM_WATCHDOG"}, {
192 BRCMF_E_PROBREQ_MSG, "PROBREQ_MSG"}, {
193 BRCMF_E_SCAN_CONFIRM_IND, "SCAN_CONFIRM_IND"}, {
194 BRCMF_E_PSK_SUP, "PSK_SUP"}, {
195 BRCMF_E_COUNTRY_CODE_CHANGED, "COUNTRY_CODE_CHANGED"}, {
196 BRCMF_E_EXCEEDED_MEDIUM_TIME, "EXCEEDED_MEDIUM_TIME"}, {
197 BRCMF_E_ICV_ERROR, "ICV_ERROR"}, {
198 BRCMF_E_UNICAST_DECODE_ERROR, "UNICAST_DECODE_ERROR"}, {
199 BRCMF_E_MULTICAST_DECODE_ERROR, "MULTICAST_DECODE_ERROR"}, {
200 BRCMF_E_TRACE, "TRACE"}, {
201 BRCMF_E_ACTION_FRAME, "ACTION FRAME"}, {
202 BRCMF_E_ACTION_FRAME_COMPLETE, "ACTION FRAME TX COMPLETE"}, {
203 BRCMF_E_IF, "IF"}, {
204 BRCMF_E_RSSI, "RSSI"}, {
205 BRCMF_E_PFN_SCAN_COMPLETE, "SCAN_COMPLETE"}
207 uint event_type, flags, auth_type, datalen;
208 static u32 seqnum_prev;
209 struct msgtrace_hdr hdr;
210 u32 nblost;
211 char *s, *p;
213 event_type = be32_to_cpu(event->event_type);
214 flags = be16_to_cpu(event->flags);
215 status = be32_to_cpu(event->status);
216 reason = be32_to_cpu(event->reason);
217 auth_type = be32_to_cpu(event->auth_type);
218 datalen = be32_to_cpu(event->datalen);
219 /* debug dump of event messages */
220 sprintf(eabuf, "%pM", event->addr);
222 event_name = "UNKNOWN";
223 for (i = 0; i < ARRAY_SIZE(event_names); i++) {
224 if (event_names[i].event == event_type)
225 event_name = event_names[i].event_name;
228 brcmf_dbg(EVENT, "EVENT: %s, event ID = %d\n", event_name, event_type);
229 brcmf_dbg(EVENT, "flags 0x%04x, status %d, reason %d, auth_type %d MAC %s\n",
230 flags, status, reason, auth_type, eabuf);
232 if (flags & BRCMF_EVENT_MSG_LINK)
233 link = true;
234 if (flags & BRCMF_EVENT_MSG_GROUP)
235 group = true;
236 if (flags & BRCMF_EVENT_MSG_FLUSHTXQ)
237 flush_txq = true;
239 switch (event_type) {
240 case BRCMF_E_START:
241 case BRCMF_E_DEAUTH:
242 case BRCMF_E_DISASSOC:
243 brcmf_dbg(EVENT, "MACEVENT: %s, MAC %s\n", event_name, eabuf);
244 break;
246 case BRCMF_E_ASSOC_IND:
247 case BRCMF_E_REASSOC_IND:
248 brcmf_dbg(EVENT, "MACEVENT: %s, MAC %s\n", event_name, eabuf);
249 break;
251 case BRCMF_E_ASSOC:
252 case BRCMF_E_REASSOC:
253 if (status == BRCMF_E_STATUS_SUCCESS)
254 brcmf_dbg(EVENT, "MACEVENT: %s, MAC %s, SUCCESS\n",
255 event_name, eabuf);
256 else if (status == BRCMF_E_STATUS_TIMEOUT)
257 brcmf_dbg(EVENT, "MACEVENT: %s, MAC %s, TIMEOUT\n",
258 event_name, eabuf);
259 else if (status == BRCMF_E_STATUS_FAIL)
260 brcmf_dbg(EVENT, "MACEVENT: %s, MAC %s, FAILURE, reason %d\n",
261 event_name, eabuf, (int)reason);
262 else
263 brcmf_dbg(EVENT, "MACEVENT: %s, MAC %s, unexpected status %d\n",
264 event_name, eabuf, (int)status);
265 break;
267 case BRCMF_E_DEAUTH_IND:
268 case BRCMF_E_DISASSOC_IND:
269 brcmf_dbg(EVENT, "MACEVENT: %s, MAC %s, reason %d\n",
270 event_name, eabuf, (int)reason);
271 break;
273 case BRCMF_E_AUTH:
274 case BRCMF_E_AUTH_IND:
275 if (auth_type == WLAN_AUTH_OPEN)
276 auth_str = "Open System";
277 else if (auth_type == WLAN_AUTH_SHARED_KEY)
278 auth_str = "Shared Key";
279 else {
280 sprintf(err_msg, "AUTH unknown: %d", (int)auth_type);
281 auth_str = err_msg;
283 if (event_type == BRCMF_E_AUTH_IND)
284 brcmf_dbg(EVENT, "MACEVENT: %s, MAC %s, %s\n",
285 event_name, eabuf, auth_str);
286 else if (status == BRCMF_E_STATUS_SUCCESS)
287 brcmf_dbg(EVENT, "MACEVENT: %s, MAC %s, %s, SUCCESS\n",
288 event_name, eabuf, auth_str);
289 else if (status == BRCMF_E_STATUS_TIMEOUT)
290 brcmf_dbg(EVENT, "MACEVENT: %s, MAC %s, %s, TIMEOUT\n",
291 event_name, eabuf, auth_str);
292 else if (status == BRCMF_E_STATUS_FAIL) {
293 brcmf_dbg(EVENT, "MACEVENT: %s, MAC %s, %s, FAILURE, reason %d\n",
294 event_name, eabuf, auth_str, (int)reason);
297 break;
299 case BRCMF_E_JOIN:
300 case BRCMF_E_ROAM:
301 case BRCMF_E_SET_SSID:
302 if (status == BRCMF_E_STATUS_SUCCESS)
303 brcmf_dbg(EVENT, "MACEVENT: %s, MAC %s\n",
304 event_name, eabuf);
305 else if (status == BRCMF_E_STATUS_FAIL)
306 brcmf_dbg(EVENT, "MACEVENT: %s, failed\n", event_name);
307 else if (status == BRCMF_E_STATUS_NO_NETWORKS)
308 brcmf_dbg(EVENT, "MACEVENT: %s, no networks found\n",
309 event_name);
310 else
311 brcmf_dbg(EVENT, "MACEVENT: %s, unexpected status %d\n",
312 event_name, (int)status);
313 break;
315 case BRCMF_E_BEACON_RX:
316 if (status == BRCMF_E_STATUS_SUCCESS)
317 brcmf_dbg(EVENT, "MACEVENT: %s, SUCCESS\n", event_name);
318 else if (status == BRCMF_E_STATUS_FAIL)
319 brcmf_dbg(EVENT, "MACEVENT: %s, FAIL\n", event_name);
320 else
321 brcmf_dbg(EVENT, "MACEVENT: %s, status %d\n",
322 event_name, status);
323 break;
325 case BRCMF_E_LINK:
326 brcmf_dbg(EVENT, "MACEVENT: %s %s\n",
327 event_name, link ? "UP" : "DOWN");
328 break;
330 case BRCMF_E_MIC_ERROR:
331 brcmf_dbg(EVENT, "MACEVENT: %s, MAC %s, Group %d, Flush %d\n",
332 event_name, eabuf, group, flush_txq);
333 break;
335 case BRCMF_E_ICV_ERROR:
336 case BRCMF_E_UNICAST_DECODE_ERROR:
337 case BRCMF_E_MULTICAST_DECODE_ERROR:
338 brcmf_dbg(EVENT, "MACEVENT: %s, MAC %s\n", event_name, eabuf);
339 break;
341 case BRCMF_E_TXFAIL:
342 brcmf_dbg(EVENT, "MACEVENT: %s, RA %s\n", event_name, eabuf);
343 break;
345 case BRCMF_E_SCAN_COMPLETE:
346 case BRCMF_E_PMKID_CACHE:
347 brcmf_dbg(EVENT, "MACEVENT: %s\n", event_name);
348 break;
350 case BRCMF_E_PFN_NET_FOUND:
351 case BRCMF_E_PFN_NET_LOST:
352 case BRCMF_E_PFN_SCAN_COMPLETE:
353 brcmf_dbg(EVENT, "PNOEVENT: %s\n", event_name);
354 break;
356 case BRCMF_E_PSK_SUP:
357 case BRCMF_E_PRUNE:
358 brcmf_dbg(EVENT, "MACEVENT: %s, status %d, reason %d\n",
359 event_name, (int)status, (int)reason);
360 break;
362 case BRCMF_E_TRACE:
363 buf = (unsigned char *) event_data;
364 memcpy(&hdr, buf, sizeof(struct msgtrace_hdr));
366 if (hdr.version != MSGTRACE_VERSION) {
367 brcmf_dbg(ERROR,
368 "MACEVENT: %s [unsupported version --> brcmf"
369 " version:%d dongle version:%d]\n",
370 event_name, MSGTRACE_VERSION, hdr.version);
371 /* Reset datalen to avoid display below */
372 datalen = 0;
373 break;
376 /* There are 2 bytes available at the end of data */
377 *(buf + sizeof(struct msgtrace_hdr)
378 + be16_to_cpu(hdr.len)) = '\0';
380 if (be32_to_cpu(hdr.discarded_bytes)
381 || be32_to_cpu(hdr.discarded_printf))
382 brcmf_dbg(ERROR,
383 "WLC_E_TRACE: [Discarded traces in dongle -->"
384 " discarded_bytes %d discarded_printf %d]\n",
385 be32_to_cpu(hdr.discarded_bytes),
386 be32_to_cpu(hdr.discarded_printf));
388 nblost = be32_to_cpu(hdr.seqnum) - seqnum_prev - 1;
389 if (nblost > 0)
390 brcmf_dbg(ERROR, "WLC_E_TRACE: [Event lost --> seqnum "
391 " %d nblost %d\n", be32_to_cpu(hdr.seqnum),
392 nblost);
393 seqnum_prev = be32_to_cpu(hdr.seqnum);
395 /* Display the trace buffer. Advance from \n to \n to
396 * avoid display big
397 * printf (issue with Linux printk )
399 p = (char *)&buf[sizeof(struct msgtrace_hdr)];
400 while ((s = strstr(p, "\n")) != NULL) {
401 *s = '\0';
402 printk(KERN_DEBUG"%s\n", p);
403 p = s + 1;
405 printk(KERN_DEBUG "%s\n", p);
407 /* Reset datalen to avoid display below */
408 datalen = 0;
409 break;
411 case BRCMF_E_RSSI:
412 brcmf_dbg(EVENT, "MACEVENT: %s %d\n",
413 event_name, be32_to_cpu(*((__be32 *)event_data)));
414 break;
416 default:
417 brcmf_dbg(EVENT,
418 "MACEVENT: %s %d, MAC %s, status %d, reason %d, "
419 "auth %d\n", event_name, event_type, eabuf,
420 (int)status, (int)reason, (int)auth_type);
421 break;
424 /* show any appended data */
425 if (datalen) {
426 buf = (unsigned char *) event_data;
427 brcmf_dbg(EVENT, " data (%d) : ", datalen);
428 for (i = 0; i < datalen; i++)
429 brcmf_dbg(EVENT, " 0x%02x ", *buf++);
430 brcmf_dbg(EVENT, "\n");
433 #endif /* BCMDBG */
436 brcmf_c_host_event(struct brcmf_pub *drvr, int *ifidx, void *pktdata,
437 struct brcmf_event_msg *event, void **data_ptr)
439 /* check whether packet is a BRCM event pkt */
440 struct brcmf_event *pvt_data = (struct brcmf_event *) pktdata;
441 struct brcmf_if_event *ifevent;
442 char *event_data;
443 u32 type, status;
444 u16 flags;
445 int evlen;
447 if (memcmp(BRCM_OUI, &pvt_data->hdr.oui[0], DOT11_OUI_LEN)) {
448 brcmf_dbg(ERROR, "mismatched OUI, bailing\n");
449 return -EBADE;
452 /* BRCM event pkt may be unaligned - use xxx_ua to load user_subtype. */
453 if (get_unaligned_be16(&pvt_data->hdr.usr_subtype) !=
454 BCMILCP_BCM_SUBTYPE_EVENT) {
455 brcmf_dbg(ERROR, "mismatched subtype, bailing\n");
456 return -EBADE;
459 *data_ptr = &pvt_data[1];
460 event_data = *data_ptr;
462 /* memcpy since BRCM event pkt may be unaligned. */
463 memcpy(event, &pvt_data->msg, sizeof(struct brcmf_event_msg));
465 type = get_unaligned_be32(&event->event_type);
466 flags = get_unaligned_be16(&event->flags);
467 status = get_unaligned_be32(&event->status);
468 evlen = get_unaligned_be32(&event->datalen) +
469 sizeof(struct brcmf_event);
471 switch (type) {
472 case BRCMF_E_IF:
473 ifevent = (struct brcmf_if_event *) event_data;
474 brcmf_dbg(TRACE, "if event\n");
476 if (ifevent->ifidx > 0 && ifevent->ifidx < BRCMF_MAX_IFS) {
477 if (ifevent->action == BRCMF_E_IF_ADD)
478 brcmf_add_if(drvr->dev, ifevent->ifidx,
479 event->ifname,
480 pvt_data->eth.h_dest);
481 else
482 brcmf_del_if(drvr, ifevent->ifidx);
483 } else {
484 brcmf_dbg(ERROR, "Invalid ifidx %d for %s\n",
485 ifevent->ifidx, event->ifname);
488 /* send up the if event: btamp user needs it */
489 *ifidx = brcmf_ifname2idx(drvr, event->ifname);
490 break;
492 /* These are what external supplicant/authenticator wants */
493 case BRCMF_E_LINK:
494 case BRCMF_E_ASSOC_IND:
495 case BRCMF_E_REASSOC_IND:
496 case BRCMF_E_DISASSOC_IND:
497 case BRCMF_E_MIC_ERROR:
498 default:
499 /* Fall through: this should get _everything_ */
501 *ifidx = brcmf_ifname2idx(drvr, event->ifname);
502 brcmf_dbg(TRACE, "MAC event %d, flags %x, status %x\n",
503 type, flags, status);
505 /* put it back to BRCMF_E_NDIS_LINK */
506 if (type == BRCMF_E_NDIS_LINK) {
507 u32 temp1;
508 __be32 temp2;
510 temp1 = get_unaligned_be32(&event->event_type);
511 brcmf_dbg(TRACE, "Converted to WLC_E_LINK type %d\n",
512 temp1);
514 temp2 = cpu_to_be32(BRCMF_E_NDIS_LINK);
515 memcpy((void *)(&pvt_data->msg.event_type), &temp2,
516 sizeof(pvt_data->msg.event_type));
518 break;
521 #ifdef BCMDBG
522 brcmf_c_show_host_event(event, event_data);
523 #endif /* BCMDBG */
525 return 0;
528 /* Convert user's input in hex pattern to byte-size mask */
529 static int brcmf_c_pattern_atoh(char *src, char *dst)
531 int i;
532 if (strncmp(src, "0x", 2) != 0 && strncmp(src, "0X", 2) != 0) {
533 brcmf_dbg(ERROR, "Mask invalid format. Needs to start with 0x\n");
534 return -EINVAL;
536 src = src + 2; /* Skip past 0x */
537 if (strlen(src) % 2 != 0) {
538 brcmf_dbg(ERROR, "Mask invalid format. Length must be even.\n");
539 return -EINVAL;
541 for (i = 0; *src != '\0'; i++) {
542 unsigned long res;
543 char num[3];
544 strncpy(num, src, 2);
545 num[2] = '\0';
546 if (kstrtoul(num, 16, &res))
547 return -EINVAL;
548 dst[i] = (u8)res;
549 src += 2;
551 return i;
554 void
555 brcmf_c_pktfilter_offload_enable(struct brcmf_pub *drvr, char *arg, int enable,
556 int master_mode)
558 unsigned long res;
559 char *argv[8];
560 int i = 0;
561 const char *str;
562 int buf_len;
563 int str_len;
564 char *arg_save = NULL, *arg_org = NULL;
565 int rc;
566 char buf[128];
567 struct brcmf_pkt_filter_enable_le enable_parm;
568 struct brcmf_pkt_filter_enable_le *pkt_filterp;
569 __le32 mmode_le;
571 arg_save = kmalloc(strlen(arg) + 1, GFP_ATOMIC);
572 if (!arg_save)
573 goto fail;
575 arg_org = arg_save;
576 memcpy(arg_save, arg, strlen(arg) + 1);
578 argv[i] = strsep(&arg_save, " ");
580 i = 0;
581 if (NULL == argv[i]) {
582 brcmf_dbg(ERROR, "No args provided\n");
583 goto fail;
586 str = "pkt_filter_enable";
587 str_len = strlen(str);
588 strncpy(buf, str, str_len);
589 buf[str_len] = '\0';
590 buf_len = str_len + 1;
592 pkt_filterp = (struct brcmf_pkt_filter_enable_le *) (buf + str_len + 1);
594 /* Parse packet filter id. */
595 enable_parm.id = 0;
596 if (!kstrtoul(argv[i], 0, &res))
597 enable_parm.id = cpu_to_le32((u32)res);
599 /* Parse enable/disable value. */
600 enable_parm.enable = cpu_to_le32(enable);
602 buf_len += sizeof(enable_parm);
603 memcpy((char *)pkt_filterp, &enable_parm, sizeof(enable_parm));
605 /* Enable/disable the specified filter. */
606 rc = brcmf_proto_cdc_set_dcmd(drvr, 0, BRCMF_C_SET_VAR, buf, buf_len);
607 rc = rc >= 0 ? 0 : rc;
608 if (rc)
609 brcmf_dbg(TRACE, "failed to add pktfilter %s, retcode = %d\n",
610 arg, rc);
611 else
612 brcmf_dbg(TRACE, "successfully added pktfilter %s\n", arg);
614 /* Contorl the master mode */
615 mmode_le = cpu_to_le32(master_mode);
616 brcmf_c_mkiovar("pkt_filter_mode", (char *)&mmode_le, 4, buf,
617 sizeof(buf));
618 rc = brcmf_proto_cdc_set_dcmd(drvr, 0, BRCMF_C_SET_VAR, buf,
619 sizeof(buf));
620 rc = rc >= 0 ? 0 : rc;
621 if (rc)
622 brcmf_dbg(TRACE, "failed to add pktfilter %s, retcode = %d\n",
623 arg, rc);
625 fail:
626 kfree(arg_org);
629 void brcmf_c_pktfilter_offload_set(struct brcmf_pub *drvr, char *arg)
631 const char *str;
632 struct brcmf_pkt_filter_le pkt_filter;
633 struct brcmf_pkt_filter_le *pkt_filterp;
634 unsigned long res;
635 int buf_len;
636 int str_len;
637 int rc;
638 u32 mask_size;
639 u32 pattern_size;
640 char *argv[8], *buf = NULL;
641 int i = 0;
642 char *arg_save = NULL, *arg_org = NULL;
644 arg_save = kstrdup(arg, GFP_ATOMIC);
645 if (!arg_save)
646 goto fail;
648 arg_org = arg_save;
650 buf = kmalloc(PKTFILTER_BUF_SIZE, GFP_ATOMIC);
651 if (!buf)
652 goto fail;
654 argv[i] = strsep(&arg_save, " ");
655 while (argv[i++])
656 argv[i] = strsep(&arg_save, " ");
658 i = 0;
659 if (NULL == argv[i]) {
660 brcmf_dbg(ERROR, "No args provided\n");
661 goto fail;
664 str = "pkt_filter_add";
665 strcpy(buf, str);
666 str_len = strlen(str);
667 buf_len = str_len + 1;
669 pkt_filterp = (struct brcmf_pkt_filter_le *) (buf + str_len + 1);
671 /* Parse packet filter id. */
672 pkt_filter.id = 0;
673 if (!kstrtoul(argv[i], 0, &res))
674 pkt_filter.id = cpu_to_le32((u32)res);
676 if (NULL == argv[++i]) {
677 brcmf_dbg(ERROR, "Polarity not provided\n");
678 goto fail;
681 /* Parse filter polarity. */
682 pkt_filter.negate_match = 0;
683 if (!kstrtoul(argv[i], 0, &res))
684 pkt_filter.negate_match = cpu_to_le32((u32)res);
686 if (NULL == argv[++i]) {
687 brcmf_dbg(ERROR, "Filter type not provided\n");
688 goto fail;
691 /* Parse filter type. */
692 pkt_filter.type = 0;
693 if (!kstrtoul(argv[i], 0, &res))
694 pkt_filter.type = cpu_to_le32((u32)res);
696 if (NULL == argv[++i]) {
697 brcmf_dbg(ERROR, "Offset not provided\n");
698 goto fail;
701 /* Parse pattern filter offset. */
702 pkt_filter.u.pattern.offset = 0;
703 if (!kstrtoul(argv[i], 0, &res))
704 pkt_filter.u.pattern.offset = cpu_to_le32((u32)res);
706 if (NULL == argv[++i]) {
707 brcmf_dbg(ERROR, "Bitmask not provided\n");
708 goto fail;
711 /* Parse pattern filter mask. */
712 mask_size =
713 brcmf_c_pattern_atoh
714 (argv[i], (char *)pkt_filterp->u.pattern.mask_and_pattern);
716 if (NULL == argv[++i]) {
717 brcmf_dbg(ERROR, "Pattern not provided\n");
718 goto fail;
721 /* Parse pattern filter pattern. */
722 pattern_size =
723 brcmf_c_pattern_atoh(argv[i],
724 (char *)&pkt_filterp->u.pattern.
725 mask_and_pattern[mask_size]);
727 if (mask_size != pattern_size) {
728 brcmf_dbg(ERROR, "Mask and pattern not the same size\n");
729 goto fail;
732 pkt_filter.u.pattern.size_bytes = cpu_to_le32(mask_size);
733 buf_len += BRCMF_PKT_FILTER_FIXED_LEN;
734 buf_len += (BRCMF_PKT_FILTER_PATTERN_FIXED_LEN + 2 * mask_size);
736 /* Keep-alive attributes are set in local
737 * variable (keep_alive_pkt), and
738 ** then memcpy'ed into buffer (keep_alive_pktp) since there is no
739 ** guarantee that the buffer is properly aligned.
741 memcpy((char *)pkt_filterp,
742 &pkt_filter,
743 BRCMF_PKT_FILTER_FIXED_LEN + BRCMF_PKT_FILTER_PATTERN_FIXED_LEN);
745 rc = brcmf_proto_cdc_set_dcmd(drvr, 0, BRCMF_C_SET_VAR, buf, buf_len);
746 rc = rc >= 0 ? 0 : rc;
748 if (rc)
749 brcmf_dbg(TRACE, "failed to add pktfilter %s, retcode = %d\n",
750 arg, rc);
751 else
752 brcmf_dbg(TRACE, "successfully added pktfilter %s\n", arg);
754 fail:
755 kfree(arg_org);
757 kfree(buf);
760 static void brcmf_c_arp_offload_set(struct brcmf_pub *drvr, int arp_mode)
762 char iovbuf[32];
763 int retcode;
765 brcmf_c_mkiovar("arp_ol", (char *)&arp_mode, 4, iovbuf, sizeof(iovbuf));
766 retcode = brcmf_proto_cdc_set_dcmd(drvr, 0, BRCMF_C_SET_VAR,
767 iovbuf, sizeof(iovbuf));
768 retcode = retcode >= 0 ? 0 : retcode;
769 if (retcode)
770 brcmf_dbg(TRACE, "failed to set ARP offload mode to 0x%x, retcode = %d\n",
771 arp_mode, retcode);
772 else
773 brcmf_dbg(TRACE, "successfully set ARP offload mode to 0x%x\n",
774 arp_mode);
777 static void brcmf_c_arp_offload_enable(struct brcmf_pub *drvr, int arp_enable)
779 char iovbuf[32];
780 int retcode;
782 brcmf_c_mkiovar("arpoe", (char *)&arp_enable, 4,
783 iovbuf, sizeof(iovbuf));
784 retcode = brcmf_proto_cdc_set_dcmd(drvr, 0, BRCMF_C_SET_VAR,
785 iovbuf, sizeof(iovbuf));
786 retcode = retcode >= 0 ? 0 : retcode;
787 if (retcode)
788 brcmf_dbg(TRACE, "failed to enable ARP offload to %d, retcode = %d\n",
789 arp_enable, retcode);
790 else
791 brcmf_dbg(TRACE, "successfully enabled ARP offload to %d\n",
792 arp_enable);
795 int brcmf_c_preinit_dcmds(struct brcmf_pub *drvr)
797 char iovbuf[BRCMF_EVENTING_MASK_LEN + 12]; /* Room for
798 "event_msgs" + '\0' + bitvec */
799 uint up = 0;
800 char buf[128], *ptr;
801 u32 dongle_align = drvr->bus_if->align;
802 u32 glom = 0;
803 u32 roaming = 1;
804 uint bcn_timeout = 3;
805 int scan_assoc_time = 40;
806 int scan_unassoc_time = 40;
807 int i;
809 mutex_lock(&drvr->proto_block);
811 /* Set Country code */
812 if (drvr->country_code[0] != 0) {
813 if (brcmf_proto_cdc_set_dcmd(drvr, 0, BRCMF_C_SET_COUNTRY,
814 drvr->country_code,
815 sizeof(drvr->country_code)) < 0)
816 brcmf_dbg(ERROR, "country code setting failed\n");
819 /* query for 'ver' to get version info from firmware */
820 memset(buf, 0, sizeof(buf));
821 ptr = buf;
822 brcmf_c_mkiovar("ver", NULL, 0, buf, sizeof(buf));
823 brcmf_proto_cdc_query_dcmd(drvr, 0, BRCMF_C_GET_VAR, buf, sizeof(buf));
824 strsep(&ptr, "\n");
825 /* Print fw version info */
826 brcmf_dbg(ERROR, "Firmware version = %s\n", buf);
828 /* Match Host and Dongle rx alignment */
829 brcmf_c_mkiovar("bus:txglomalign", (char *)&dongle_align, 4, iovbuf,
830 sizeof(iovbuf));
831 brcmf_proto_cdc_set_dcmd(drvr, 0, BRCMF_C_SET_VAR, iovbuf,
832 sizeof(iovbuf));
834 /* disable glom option per default */
835 brcmf_c_mkiovar("bus:txglom", (char *)&glom, 4, iovbuf, sizeof(iovbuf));
836 brcmf_proto_cdc_set_dcmd(drvr, 0, BRCMF_C_SET_VAR, iovbuf,
837 sizeof(iovbuf));
839 /* Setup timeout if Beacons are lost and roam is off to report
840 link down */
841 brcmf_c_mkiovar("bcn_timeout", (char *)&bcn_timeout, 4, iovbuf,
842 sizeof(iovbuf));
843 brcmf_proto_cdc_set_dcmd(drvr, 0, BRCMF_C_SET_VAR, iovbuf,
844 sizeof(iovbuf));
846 /* Enable/Disable build-in roaming to allowed ext supplicant to take
847 of romaing */
848 brcmf_c_mkiovar("roam_off", (char *)&roaming, 4,
849 iovbuf, sizeof(iovbuf));
850 brcmf_proto_cdc_set_dcmd(drvr, 0, BRCMF_C_SET_VAR, iovbuf,
851 sizeof(iovbuf));
853 /* Force STA UP */
854 brcmf_proto_cdc_set_dcmd(drvr, 0, BRCMF_C_UP, (char *)&up, sizeof(up));
856 /* Setup event_msgs */
857 brcmf_c_mkiovar("event_msgs", drvr->eventmask, BRCMF_EVENTING_MASK_LEN,
858 iovbuf, sizeof(iovbuf));
859 brcmf_proto_cdc_set_dcmd(drvr, 0, BRCMF_C_SET_VAR, iovbuf,
860 sizeof(iovbuf));
862 brcmf_proto_cdc_set_dcmd(drvr, 0, BRCMF_C_SET_SCAN_CHANNEL_TIME,
863 (char *)&scan_assoc_time, sizeof(scan_assoc_time));
864 brcmf_proto_cdc_set_dcmd(drvr, 0, BRCMF_C_SET_SCAN_UNASSOC_TIME,
865 (char *)&scan_unassoc_time, sizeof(scan_unassoc_time));
867 /* Set and enable ARP offload feature */
868 brcmf_c_arp_offload_set(drvr, BRCMF_ARPOL_MODE);
869 brcmf_c_arp_offload_enable(drvr, true);
871 /* Set up pkt filter */
872 for (i = 0; i < drvr->pktfilter_count; i++) {
873 brcmf_c_pktfilter_offload_set(drvr, drvr->pktfilter[i]);
874 brcmf_c_pktfilter_offload_enable(drvr, drvr->pktfilter[i],
875 0, true);
878 mutex_unlock(&drvr->proto_block);
880 return 0;