Staging: unisys: Remove RETINT macro
[linux/fpc-iii.git] / drivers / net / wireless / brcm80211 / brcmfmac / dhd_common.c
blob6a8983a1fb9c3451908c1cf28426d78e6600d091
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.
17 #include <linux/kernel.h>
18 #include <linux/string.h>
19 #include <linux/netdevice.h>
20 #include <brcmu_wifi.h>
21 #include <brcmu_utils.h>
22 #include "dhd.h"
23 #include "dhd_bus.h"
24 #include "dhd_dbg.h"
25 #include "fwil.h"
26 #include "fwil_types.h"
27 #include "tracepoint.h"
29 #define PKTFILTER_BUF_SIZE 128
30 #define BRCMF_DEFAULT_BCN_TIMEOUT 3
31 #define BRCMF_DEFAULT_SCAN_CHANNEL_TIME 40
32 #define BRCMF_DEFAULT_SCAN_UNASSOC_TIME 40
33 #define BRCMF_DEFAULT_PACKET_FILTER "100 0 0 0 0x01 0x00"
36 bool brcmf_c_prec_enq(struct device *dev, struct pktq *q,
37 struct sk_buff *pkt, int prec)
39 struct sk_buff *p;
40 int eprec = -1; /* precedence to evict from */
41 bool discard_oldest;
42 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
43 struct brcmf_pub *drvr = bus_if->drvr;
45 /* Fast case, precedence queue is not full and we are also not
46 * exceeding total queue length
48 if (!pktq_pfull(q, prec) && !pktq_full(q)) {
49 brcmu_pktq_penq(q, prec, pkt);
50 return true;
53 /* Determine precedence from which to evict packet, if any */
54 if (pktq_pfull(q, prec))
55 eprec = prec;
56 else if (pktq_full(q)) {
57 p = brcmu_pktq_peek_tail(q, &eprec);
58 if (eprec > prec)
59 return false;
62 /* Evict if needed */
63 if (eprec >= 0) {
64 /* Detect queueing to unconfigured precedence */
65 discard_oldest = ac_bitmap_tst(drvr->wme_dp, eprec);
66 if (eprec == prec && !discard_oldest)
67 return false; /* refuse newer (incoming) packet */
68 /* Evict packet according to discard policy */
69 p = discard_oldest ? brcmu_pktq_pdeq(q, eprec) :
70 brcmu_pktq_pdeq_tail(q, eprec);
71 if (p == NULL)
72 brcmf_err("brcmu_pktq_penq() failed, oldest %d\n",
73 discard_oldest);
75 brcmu_pkt_buf_free_skb(p);
78 /* Enqueue */
79 p = brcmu_pktq_penq(q, prec, pkt);
80 if (p == NULL)
81 brcmf_err("brcmu_pktq_penq() failed\n");
83 return p != NULL;
86 /* Convert user's input in hex pattern to byte-size mask */
87 static int brcmf_c_pattern_atoh(char *src, char *dst)
89 int i;
90 if (strncmp(src, "0x", 2) != 0 && strncmp(src, "0X", 2) != 0) {
91 brcmf_err("Mask invalid format. Needs to start with 0x\n");
92 return -EINVAL;
94 src = src + 2; /* Skip past 0x */
95 if (strlen(src) % 2 != 0) {
96 brcmf_err("Mask invalid format. Length must be even.\n");
97 return -EINVAL;
99 for (i = 0; *src != '\0'; i++) {
100 unsigned long res;
101 char num[3];
102 strncpy(num, src, 2);
103 num[2] = '\0';
104 if (kstrtoul(num, 16, &res))
105 return -EINVAL;
106 dst[i] = (u8)res;
107 src += 2;
109 return i;
112 static void
113 brcmf_c_pktfilter_offload_enable(struct brcmf_if *ifp, char *arg, int enable,
114 int master_mode)
116 unsigned long res;
117 char *argv;
118 char *arg_save = NULL, *arg_org = NULL;
119 s32 err;
120 struct brcmf_pkt_filter_enable_le enable_parm;
122 arg_save = kstrdup(arg, GFP_ATOMIC);
123 if (!arg_save)
124 goto fail;
126 arg_org = arg_save;
128 argv = strsep(&arg_save, " ");
130 if (argv == NULL) {
131 brcmf_err("No args provided\n");
132 goto fail;
135 /* Parse packet filter id. */
136 enable_parm.id = 0;
137 if (!kstrtoul(argv, 0, &res))
138 enable_parm.id = cpu_to_le32((u32)res);
140 /* Enable/disable the specified filter. */
141 enable_parm.enable = cpu_to_le32(enable);
143 err = brcmf_fil_iovar_data_set(ifp, "pkt_filter_enable", &enable_parm,
144 sizeof(enable_parm));
145 if (err)
146 brcmf_err("Set pkt_filter_enable error (%d)\n", err);
148 /* Control the master mode */
149 err = brcmf_fil_iovar_int_set(ifp, "pkt_filter_mode", master_mode);
150 if (err)
151 brcmf_err("Set pkt_filter_mode error (%d)\n", err);
153 fail:
154 kfree(arg_org);
157 static void brcmf_c_pktfilter_offload_set(struct brcmf_if *ifp, char *arg)
159 struct brcmf_pkt_filter_le *pkt_filter;
160 unsigned long res;
161 int buf_len;
162 s32 err;
163 u32 mask_size;
164 u32 pattern_size;
165 char *argv[8], *buf = NULL;
166 int i = 0;
167 char *arg_save = NULL, *arg_org = NULL;
169 arg_save = kstrdup(arg, GFP_ATOMIC);
170 if (!arg_save)
171 goto fail;
173 arg_org = arg_save;
175 buf = kmalloc(PKTFILTER_BUF_SIZE, GFP_ATOMIC);
176 if (!buf)
177 goto fail;
179 argv[i] = strsep(&arg_save, " ");
180 while (argv[i]) {
181 i++;
182 if (i >= 8) {
183 brcmf_err("Too many parameters\n");
184 goto fail;
186 argv[i] = strsep(&arg_save, " ");
189 if (i != 6) {
190 brcmf_err("Not enough args provided %d\n", i);
191 goto fail;
194 pkt_filter = (struct brcmf_pkt_filter_le *)buf;
196 /* Parse packet filter id. */
197 pkt_filter->id = 0;
198 if (!kstrtoul(argv[0], 0, &res))
199 pkt_filter->id = cpu_to_le32((u32)res);
201 /* Parse filter polarity. */
202 pkt_filter->negate_match = 0;
203 if (!kstrtoul(argv[1], 0, &res))
204 pkt_filter->negate_match = cpu_to_le32((u32)res);
206 /* Parse filter type. */
207 pkt_filter->type = 0;
208 if (!kstrtoul(argv[2], 0, &res))
209 pkt_filter->type = cpu_to_le32((u32)res);
211 /* Parse pattern filter offset. */
212 pkt_filter->u.pattern.offset = 0;
213 if (!kstrtoul(argv[3], 0, &res))
214 pkt_filter->u.pattern.offset = cpu_to_le32((u32)res);
216 /* Parse pattern filter mask. */
217 mask_size = brcmf_c_pattern_atoh(argv[4],
218 (char *)pkt_filter->u.pattern.mask_and_pattern);
220 /* Parse pattern filter pattern. */
221 pattern_size = brcmf_c_pattern_atoh(argv[5],
222 (char *)&pkt_filter->u.pattern.mask_and_pattern[mask_size]);
224 if (mask_size != pattern_size) {
225 brcmf_err("Mask and pattern not the same size\n");
226 goto fail;
229 pkt_filter->u.pattern.size_bytes = cpu_to_le32(mask_size);
230 buf_len = offsetof(struct brcmf_pkt_filter_le,
231 u.pattern.mask_and_pattern);
232 buf_len += mask_size + pattern_size;
234 err = brcmf_fil_iovar_data_set(ifp, "pkt_filter_add", pkt_filter,
235 buf_len);
236 if (err)
237 brcmf_err("Set pkt_filter_add error (%d)\n", err);
239 fail:
240 kfree(arg_org);
242 kfree(buf);
245 int brcmf_c_preinit_dcmds(struct brcmf_if *ifp)
247 s8 eventmask[BRCMF_EVENTING_MASK_LEN];
248 u8 buf[BRCMF_DCMD_SMLEN];
249 char *ptr;
250 s32 err;
252 /* retreive mac address */
253 err = brcmf_fil_iovar_data_get(ifp, "cur_etheraddr", ifp->mac_addr,
254 sizeof(ifp->mac_addr));
255 if (err < 0) {
256 brcmf_err("Retreiving cur_etheraddr failed, %d\n",
257 err);
258 goto done;
260 memcpy(ifp->drvr->mac, ifp->mac_addr, sizeof(ifp->drvr->mac));
262 /* query for 'ver' to get version info from firmware */
263 memset(buf, 0, sizeof(buf));
264 strcpy(buf, "ver");
265 err = brcmf_fil_iovar_data_get(ifp, "ver", buf, sizeof(buf));
266 if (err < 0) {
267 brcmf_err("Retreiving version information failed, %d\n",
268 err);
269 goto done;
271 ptr = (char *)buf;
272 strsep(&ptr, "\n");
274 /* Print fw version info */
275 brcmf_err("Firmware version = %s\n", buf);
277 /* locate firmware version number for ethtool */
278 ptr = strrchr(buf, ' ') + 1;
279 strlcpy(ifp->drvr->fwver, ptr, sizeof(ifp->drvr->fwver));
282 * Setup timeout if Beacons are lost and roam is off to report
283 * link down
285 err = brcmf_fil_iovar_int_set(ifp, "bcn_timeout",
286 BRCMF_DEFAULT_BCN_TIMEOUT);
287 if (err) {
288 brcmf_err("bcn_timeout error (%d)\n", err);
289 goto done;
292 /* Enable/Disable build-in roaming to allowed ext supplicant to take
293 * of romaing
295 err = brcmf_fil_iovar_int_set(ifp, "roam_off", 1);
296 if (err) {
297 brcmf_err("roam_off error (%d)\n", err);
298 goto done;
301 /* Setup event_msgs, enable E_IF */
302 err = brcmf_fil_iovar_data_get(ifp, "event_msgs", eventmask,
303 BRCMF_EVENTING_MASK_LEN);
304 if (err) {
305 brcmf_err("Get event_msgs error (%d)\n", err);
306 goto done;
308 setbit(eventmask, BRCMF_E_IF);
309 err = brcmf_fil_iovar_data_set(ifp, "event_msgs", eventmask,
310 BRCMF_EVENTING_MASK_LEN);
311 if (err) {
312 brcmf_err("Set event_msgs error (%d)\n", err);
313 goto done;
316 /* Setup default scan channel time */
317 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCAN_CHANNEL_TIME,
318 BRCMF_DEFAULT_SCAN_CHANNEL_TIME);
319 if (err) {
320 brcmf_err("BRCMF_C_SET_SCAN_CHANNEL_TIME error (%d)\n",
321 err);
322 goto done;
325 /* Setup default scan unassoc time */
326 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCAN_UNASSOC_TIME,
327 BRCMF_DEFAULT_SCAN_UNASSOC_TIME);
328 if (err) {
329 brcmf_err("BRCMF_C_SET_SCAN_UNASSOC_TIME error (%d)\n",
330 err);
331 goto done;
334 /* Setup packet filter */
335 brcmf_c_pktfilter_offload_set(ifp, BRCMF_DEFAULT_PACKET_FILTER);
336 brcmf_c_pktfilter_offload_enable(ifp, BRCMF_DEFAULT_PACKET_FILTER,
337 0, true);
339 /* do bus specific preinit here */
340 err = brcmf_bus_preinit(ifp->drvr->bus_if);
341 done:
342 return err;
345 #ifdef CONFIG_BRCM_TRACING
346 void __brcmf_err(const char *func, const char *fmt, ...)
348 struct va_format vaf = {
349 .fmt = fmt,
351 va_list args;
353 va_start(args, fmt);
354 vaf.va = &args;
355 pr_err("%s: %pV", func, &vaf);
356 trace_brcmf_err(func, &vaf);
357 va_end(args);
359 #endif
360 #if defined(CONFIG_BRCM_TRACING) || defined(CONFIG_BRCMDBG)
361 void __brcmf_dbg(u32 level, const char *func, const char *fmt, ...)
363 struct va_format vaf = {
364 .fmt = fmt,
366 va_list args;
368 va_start(args, fmt);
369 vaf.va = &args;
370 if (brcmf_msg_level & level)
371 pr_debug("%s %pV", func, &vaf);
372 trace_brcmf_dbg(level, func, &vaf);
373 va_end(args);
375 #endif