initial commit with v3.6.7
[linux-3.6.7-moxart.git] / drivers / net / wireless / brcm80211 / brcmutil / utils.c
blob3e6405e06ac028687e1d12627874491e969d6e94
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 #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)
31 struct sk_buff *skb;
33 skb = dev_alloc_skb(len);
34 if (skb) {
35 skb_put(skb, len);
36 skb->priority = 0;
39 return skb;
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)
46 if (!skb)
47 return;
48 WARN_ON(skb->next);
49 if (skb->destructor)
50 /* cannot kfree_skb() on hard IRQ (net/core/skbuff.c) if
51 * destructor exists
53 dev_kfree_skb_any(skb);
54 else
55 /* can free immediately (even in_irq()) if destructor
56 * does not exist
58 dev_kfree_skb(skb);
60 EXPORT_SYMBOL(brcmu_pkt_buf_free_skb);
63 * osl multiple-precedence packet queue
64 * hi_prec is always >= the number of the highest non-empty precedence
66 struct sk_buff *brcmu_pktq_penq(struct pktq *pq, int prec,
67 struct sk_buff *p)
69 struct sk_buff_head *q;
71 if (pktq_full(pq) || pktq_pfull(pq, prec))
72 return NULL;
74 q = &pq->q[prec].skblist;
75 skb_queue_tail(q, p);
76 pq->len++;
78 if (pq->hi_prec < prec)
79 pq->hi_prec = (u8) prec;
81 return p;
83 EXPORT_SYMBOL(brcmu_pktq_penq);
85 struct sk_buff *brcmu_pktq_penq_head(struct pktq *pq, int prec,
86 struct sk_buff *p)
88 struct sk_buff_head *q;
90 if (pktq_full(pq) || pktq_pfull(pq, prec))
91 return NULL;
93 q = &pq->q[prec].skblist;
94 skb_queue_head(q, p);
95 pq->len++;
97 if (pq->hi_prec < prec)
98 pq->hi_prec = (u8) prec;
100 return p;
102 EXPORT_SYMBOL(brcmu_pktq_penq_head);
104 struct sk_buff *brcmu_pktq_pdeq(struct pktq *pq, int prec)
106 struct sk_buff_head *q;
107 struct sk_buff *p;
109 q = &pq->q[prec].skblist;
110 p = skb_dequeue(q);
111 if (p == NULL)
112 return NULL;
114 pq->len--;
115 return p;
117 EXPORT_SYMBOL(brcmu_pktq_pdeq);
119 struct sk_buff *brcmu_pktq_pdeq_tail(struct pktq *pq, int prec)
121 struct sk_buff_head *q;
122 struct sk_buff *p;
124 q = &pq->q[prec].skblist;
125 p = skb_dequeue_tail(q);
126 if (p == NULL)
127 return NULL;
129 pq->len--;
130 return p;
132 EXPORT_SYMBOL(brcmu_pktq_pdeq_tail);
134 void
135 brcmu_pktq_pflush(struct pktq *pq, int prec, bool dir,
136 bool (*fn)(struct sk_buff *, void *), void *arg)
138 struct sk_buff_head *q;
139 struct sk_buff *p, *next;
141 q = &pq->q[prec].skblist;
142 skb_queue_walk_safe(q, p, next) {
143 if (fn == NULL || (*fn) (p, arg)) {
144 skb_unlink(p, q);
145 brcmu_pkt_buf_free_skb(p);
146 pq->len--;
150 EXPORT_SYMBOL(brcmu_pktq_pflush);
152 void brcmu_pktq_flush(struct pktq *pq, bool dir,
153 bool (*fn)(struct sk_buff *, void *), void *arg)
155 int prec;
156 for (prec = 0; prec < pq->num_prec; prec++)
157 brcmu_pktq_pflush(pq, prec, dir, fn, arg);
159 EXPORT_SYMBOL(brcmu_pktq_flush);
161 void brcmu_pktq_init(struct pktq *pq, int num_prec, int max_len)
163 int prec;
165 /* pq is variable size; only zero out what's requested */
166 memset(pq, 0,
167 offsetof(struct pktq, q) + (sizeof(struct pktq_prec) * num_prec));
169 pq->num_prec = (u16) num_prec;
171 pq->max = (u16) max_len;
173 for (prec = 0; prec < num_prec; prec++) {
174 pq->q[prec].max = pq->max;
175 skb_queue_head_init(&pq->q[prec].skblist);
178 EXPORT_SYMBOL(brcmu_pktq_init);
180 struct sk_buff *brcmu_pktq_peek_tail(struct pktq *pq, int *prec_out)
182 int prec;
184 if (pq->len == 0)
185 return NULL;
187 for (prec = 0; prec < pq->hi_prec; prec++)
188 if (!skb_queue_empty(&pq->q[prec].skblist))
189 break;
191 if (prec_out)
192 *prec_out = prec;
194 return skb_peek_tail(&pq->q[prec].skblist);
196 EXPORT_SYMBOL(brcmu_pktq_peek_tail);
198 /* Return sum of lengths of a specific set of precedences */
199 int brcmu_pktq_mlen(struct pktq *pq, uint prec_bmp)
201 int prec, len;
203 len = 0;
205 for (prec = 0; prec <= pq->hi_prec; prec++)
206 if (prec_bmp & (1 << prec))
207 len += pq->q[prec].skblist.qlen;
209 return len;
211 EXPORT_SYMBOL(brcmu_pktq_mlen);
213 /* Priority dequeue from a specific set of precedences */
214 struct sk_buff *brcmu_pktq_mdeq(struct pktq *pq, uint prec_bmp,
215 int *prec_out)
217 struct sk_buff_head *q;
218 struct sk_buff *p;
219 int prec;
221 if (pq->len == 0)
222 return NULL;
224 while ((prec = pq->hi_prec) > 0 &&
225 skb_queue_empty(&pq->q[prec].skblist))
226 pq->hi_prec--;
228 while ((prec_bmp & (1 << prec)) == 0 ||
229 skb_queue_empty(&pq->q[prec].skblist))
230 if (prec-- == 0)
231 return NULL;
233 q = &pq->q[prec].skblist;
234 p = skb_dequeue(q);
235 if (p == NULL)
236 return NULL;
238 pq->len--;
240 if (prec_out)
241 *prec_out = prec;
243 return p;
245 EXPORT_SYMBOL(brcmu_pktq_mdeq);
247 #if defined(DEBUG)
248 /* pretty hex print a pkt buffer chain */
249 void brcmu_prpkt(const char *msg, struct sk_buff *p0)
251 struct sk_buff *p;
253 if (msg && (msg[0] != '\0'))
254 pr_debug("%s:\n", msg);
256 for (p = p0; p; p = p->next)
257 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, p->data, p->len);
259 EXPORT_SYMBOL(brcmu_prpkt);
261 void brcmu_dbg_hex_dump(const void *data, size_t size, const char *fmt, ...)
263 struct va_format vaf;
264 va_list args;
266 va_start(args, fmt);
268 vaf.fmt = fmt;
269 vaf.va = &args;
271 pr_debug("%pV", &vaf);
273 va_end(args);
275 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, data, size);
277 EXPORT_SYMBOL(brcmu_dbg_hex_dump);
278 #endif /* defined(DEBUG) */