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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/netdevice.h>
20 #include <linux/module.h>
22 #include <brcmu_utils.h>
24 MODULE_AUTHOR("Broadcom Corporation");
25 MODULE_DESCRIPTION("Broadcom 802.11n wireless LAN driver utilities.");
26 MODULE_SUPPORTED_DEVICE("Broadcom 802.11n WLAN cards");
27 MODULE_LICENSE("Dual BSD/GPL");
29 struct sk_buff
*brcmu_pkt_buf_get_skb(uint len
)
33 skb
= dev_alloc_skb(len
);
41 EXPORT_SYMBOL(brcmu_pkt_buf_get_skb
);
43 /* Free the driver packet. Free the tag if present */
44 void brcmu_pkt_buf_free_skb(struct sk_buff
*skb
)
50 dev_kfree_skb_any(skb
);
52 EXPORT_SYMBOL(brcmu_pkt_buf_free_skb
);
55 * osl multiple-precedence packet queue
56 * hi_prec is always >= the number of the highest non-empty precedence
58 struct sk_buff
*brcmu_pktq_penq(struct pktq
*pq
, int prec
,
61 struct sk_buff_head
*q
;
63 if (pktq_full(pq
) || pktq_pfull(pq
, prec
))
66 q
= &pq
->q
[prec
].skblist
;
70 if (pq
->hi_prec
< prec
)
71 pq
->hi_prec
= (u8
) prec
;
75 EXPORT_SYMBOL(brcmu_pktq_penq
);
77 struct sk_buff
*brcmu_pktq_penq_head(struct pktq
*pq
, int prec
,
80 struct sk_buff_head
*q
;
82 if (pktq_full(pq
) || pktq_pfull(pq
, prec
))
85 q
= &pq
->q
[prec
].skblist
;
89 if (pq
->hi_prec
< prec
)
90 pq
->hi_prec
= (u8
) prec
;
94 EXPORT_SYMBOL(brcmu_pktq_penq_head
);
96 struct sk_buff
*brcmu_pktq_pdeq(struct pktq
*pq
, int prec
)
98 struct sk_buff_head
*q
;
101 q
= &pq
->q
[prec
].skblist
;
109 EXPORT_SYMBOL(brcmu_pktq_pdeq
);
112 * precedence based dequeue with match function. Passing a NULL pointer
113 * for the match function parameter is considered to be a wildcard so
114 * any packet on the queue is returned. In that case it is no different
115 * from brcmu_pktq_pdeq() above.
117 struct sk_buff
*brcmu_pktq_pdeq_match(struct pktq
*pq
, int prec
,
118 bool (*match_fn
)(struct sk_buff
*skb
,
119 void *arg
), void *arg
)
121 struct sk_buff_head
*q
;
122 struct sk_buff
*p
, *next
;
124 q
= &pq
->q
[prec
].skblist
;
125 skb_queue_walk_safe(q
, p
, next
) {
126 if (match_fn
== NULL
|| match_fn(p
, arg
)) {
134 EXPORT_SYMBOL(brcmu_pktq_pdeq_match
);
136 struct sk_buff
*brcmu_pktq_pdeq_tail(struct pktq
*pq
, int prec
)
138 struct sk_buff_head
*q
;
141 q
= &pq
->q
[prec
].skblist
;
142 p
= skb_dequeue_tail(q
);
149 EXPORT_SYMBOL(brcmu_pktq_pdeq_tail
);
152 brcmu_pktq_pflush(struct pktq
*pq
, int prec
, bool dir
,
153 bool (*fn
)(struct sk_buff
*, void *), void *arg
)
155 struct sk_buff_head
*q
;
156 struct sk_buff
*p
, *next
;
158 q
= &pq
->q
[prec
].skblist
;
159 skb_queue_walk_safe(q
, p
, next
) {
160 if (fn
== NULL
|| (*fn
) (p
, arg
)) {
162 brcmu_pkt_buf_free_skb(p
);
167 EXPORT_SYMBOL(brcmu_pktq_pflush
);
169 void brcmu_pktq_flush(struct pktq
*pq
, bool dir
,
170 bool (*fn
)(struct sk_buff
*, void *), void *arg
)
173 for (prec
= 0; prec
< pq
->num_prec
; prec
++)
174 brcmu_pktq_pflush(pq
, prec
, dir
, fn
, arg
);
176 EXPORT_SYMBOL(brcmu_pktq_flush
);
178 void brcmu_pktq_init(struct pktq
*pq
, int num_prec
, int max_len
)
182 /* pq is variable size; only zero out what's requested */
184 offsetof(struct pktq
, q
) + (sizeof(struct pktq_prec
) * num_prec
));
186 pq
->num_prec
= (u16
) num_prec
;
188 pq
->max
= (u16
) max_len
;
190 for (prec
= 0; prec
< num_prec
; prec
++) {
191 pq
->q
[prec
].max
= pq
->max
;
192 skb_queue_head_init(&pq
->q
[prec
].skblist
);
195 EXPORT_SYMBOL(brcmu_pktq_init
);
197 struct sk_buff
*brcmu_pktq_peek_tail(struct pktq
*pq
, int *prec_out
)
204 for (prec
= 0; prec
< pq
->hi_prec
; prec
++)
205 if (!skb_queue_empty(&pq
->q
[prec
].skblist
))
211 return skb_peek_tail(&pq
->q
[prec
].skblist
);
213 EXPORT_SYMBOL(brcmu_pktq_peek_tail
);
215 /* Return sum of lengths of a specific set of precedences */
216 int brcmu_pktq_mlen(struct pktq
*pq
, uint prec_bmp
)
222 for (prec
= 0; prec
<= pq
->hi_prec
; prec
++)
223 if (prec_bmp
& (1 << prec
))
224 len
+= pq
->q
[prec
].skblist
.qlen
;
228 EXPORT_SYMBOL(brcmu_pktq_mlen
);
230 /* Priority dequeue from a specific set of precedences */
231 struct sk_buff
*brcmu_pktq_mdeq(struct pktq
*pq
, uint prec_bmp
,
234 struct sk_buff_head
*q
;
241 while ((prec
= pq
->hi_prec
) > 0 &&
242 skb_queue_empty(&pq
->q
[prec
].skblist
))
245 while ((prec_bmp
& (1 << prec
)) == 0 ||
246 skb_queue_empty(&pq
->q
[prec
].skblist
))
250 q
= &pq
->q
[prec
].skblist
;
262 EXPORT_SYMBOL(brcmu_pktq_mdeq
);
264 /* Produce a human-readable string for boardrev */
265 char *brcmu_boardrev_str(u32 brev
, char *buf
)
270 snprintf(buf
, BRCMU_BOARDREV_LEN
, "%d.%d",
271 (brev
& 0xf0) >> 4, brev
& 0xf);
273 c
= (brev
& 0xf000) == 0x1000 ? 'P' : 'A';
274 snprintf(buf
, BRCMU_BOARDREV_LEN
, "%c%03x", c
, brev
& 0xfff);
278 EXPORT_SYMBOL(brcmu_boardrev_str
);
280 char *brcmu_dotrev_str(u32 dotrev
, char *buf
)
285 snprintf(buf
, BRCMU_DOTREV_LEN
, "unknown");
288 dotval
[0] = (dotrev
>> 24) & 0xFF;
289 dotval
[1] = (dotrev
>> 16) & 0xFF;
290 dotval
[2] = (dotrev
>> 8) & 0xFF;
291 dotval
[3] = dotrev
& 0xFF;
294 snprintf(buf
, BRCMU_DOTREV_LEN
, "%d.%d.%d.%d", dotval
[0],
295 dotval
[1], dotval
[2], dotval
[3]);
297 snprintf(buf
, BRCMU_DOTREV_LEN
, "%d.%d.%d", dotval
[0],
298 dotval
[1], dotval
[2]);
300 snprintf(buf
, BRCMU_DOTREV_LEN
, "%d.%d", dotval
[0],
305 EXPORT_SYMBOL(brcmu_dotrev_str
);
308 /* pretty hex print a pkt buffer chain */
309 void brcmu_prpkt(const char *msg
, struct sk_buff
*p0
)
313 if (msg
&& (msg
[0] != '\0'))
314 pr_debug("%s:\n", msg
);
316 for (p
= p0
; p
; p
= p
->next
)
317 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET
, p
->data
, p
->len
);
319 EXPORT_SYMBOL(brcmu_prpkt
);
321 void brcmu_dbg_hex_dump(const void *data
, size_t size
, const char *fmt
, ...)
323 struct va_format vaf
;
331 pr_debug("%pV", &vaf
);
335 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET
, data
, size
);
337 EXPORT_SYMBOL(brcmu_dbg_hex_dump
);
339 #endif /* defined(DEBUG) */