Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-btrfs-devel.git] / drivers / net / wireless / brcm80211 / brcmutil / utils.c
blobf27c489108273281f96f5d2c3be8bd1c9ee70bf4
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/netdevice.h>
18 #include <linux/module.h>
19 #include <brcmu_utils.h>
21 MODULE_AUTHOR("Broadcom Corporation");
22 MODULE_DESCRIPTION("Broadcom 802.11n wireless LAN driver utilities.");
23 MODULE_SUPPORTED_DEVICE("Broadcom 802.11n WLAN cards");
24 MODULE_LICENSE("Dual BSD/GPL");
26 struct sk_buff *brcmu_pkt_buf_get_skb(uint len)
28 struct sk_buff *skb;
30 skb = dev_alloc_skb(len);
31 if (skb) {
32 skb_put(skb, len);
33 skb->priority = 0;
36 return skb;
38 EXPORT_SYMBOL(brcmu_pkt_buf_get_skb);
40 /* Free the driver packet. Free the tag if present */
41 void brcmu_pkt_buf_free_skb(struct sk_buff *skb)
43 struct sk_buff *nskb;
44 int nest = 0;
46 /* perversion: we use skb->next to chain multi-skb packets */
47 while (skb) {
48 nskb = skb->next;
49 skb->next = NULL;
51 if (skb->destructor)
52 /* cannot kfree_skb() on hard IRQ (net/core/skbuff.c) if
53 * destructor exists
55 dev_kfree_skb_any(skb);
56 else
57 /* can free immediately (even in_irq()) if destructor
58 * does not exist
60 dev_kfree_skb(skb);
62 nest++;
63 skb = nskb;
66 EXPORT_SYMBOL(brcmu_pkt_buf_free_skb);
69 /* copy a buffer into a pkt buffer chain */
70 uint brcmu_pktfrombuf(struct sk_buff *p, uint offset, int len,
71 unsigned char *buf)
73 uint n, ret = 0;
75 /* skip 'offset' bytes */
76 for (; p && offset; p = p->next) {
77 if (offset < (uint) (p->len))
78 break;
79 offset -= p->len;
82 if (!p)
83 return 0;
85 /* copy the data */
86 for (; p && len; p = p->next) {
87 n = min((uint) (p->len) - offset, (uint) len);
88 memcpy(p->data + offset, buf, n);
89 buf += n;
90 len -= n;
91 ret += n;
92 offset = 0;
95 return ret;
97 EXPORT_SYMBOL(brcmu_pktfrombuf);
99 /* return total length of buffer chain */
100 uint brcmu_pkttotlen(struct sk_buff *p)
102 uint total;
104 total = 0;
105 for (; p; p = p->next)
106 total += p->len;
107 return total;
109 EXPORT_SYMBOL(brcmu_pkttotlen);
112 * osl multiple-precedence packet queue
113 * hi_prec is always >= the number of the highest non-empty precedence
115 struct sk_buff *brcmu_pktq_penq(struct pktq *pq, int prec,
116 struct sk_buff *p)
118 struct pktq_prec *q;
120 if (pktq_full(pq) || pktq_pfull(pq, prec))
121 return NULL;
123 q = &pq->q[prec];
125 if (q->head)
126 q->tail->prev = p;
127 else
128 q->head = p;
130 q->tail = p;
131 q->len++;
133 pq->len++;
135 if (pq->hi_prec < prec)
136 pq->hi_prec = (u8) prec;
138 return p;
140 EXPORT_SYMBOL(brcmu_pktq_penq);
142 struct sk_buff *brcmu_pktq_penq_head(struct pktq *pq, int prec,
143 struct sk_buff *p)
145 struct pktq_prec *q;
147 if (pktq_full(pq) || pktq_pfull(pq, prec))
148 return NULL;
150 q = &pq->q[prec];
152 if (q->head == NULL)
153 q->tail = p;
155 p->prev = q->head;
156 q->head = p;
157 q->len++;
159 pq->len++;
161 if (pq->hi_prec < prec)
162 pq->hi_prec = (u8) prec;
164 return p;
166 EXPORT_SYMBOL(brcmu_pktq_penq_head);
168 struct sk_buff *brcmu_pktq_pdeq(struct pktq *pq, int prec)
170 struct pktq_prec *q;
171 struct sk_buff *p;
173 q = &pq->q[prec];
175 p = q->head;
176 if (p == NULL)
177 return NULL;
179 q->head = p->prev;
180 if (q->head == NULL)
181 q->tail = NULL;
183 q->len--;
185 pq->len--;
187 p->prev = NULL;
189 return p;
191 EXPORT_SYMBOL(brcmu_pktq_pdeq);
193 struct sk_buff *brcmu_pktq_pdeq_tail(struct pktq *pq, int prec)
195 struct pktq_prec *q;
196 struct sk_buff *p, *prev;
198 q = &pq->q[prec];
200 p = q->head;
201 if (p == NULL)
202 return NULL;
204 for (prev = NULL; p != q->tail; p = p->prev)
205 prev = p;
207 if (prev)
208 prev->prev = NULL;
209 else
210 q->head = NULL;
212 q->tail = prev;
213 q->len--;
215 pq->len--;
217 return p;
219 EXPORT_SYMBOL(brcmu_pktq_pdeq_tail);
221 void
222 brcmu_pktq_pflush(struct pktq *pq, int prec, bool dir,
223 bool (*fn)(struct sk_buff *, void *), void *arg)
225 struct pktq_prec *q;
226 struct sk_buff *p, *prev = NULL;
228 q = &pq->q[prec];
229 p = q->head;
230 while (p) {
231 if (fn == NULL || (*fn) (p, arg)) {
232 bool head = (p == q->head);
233 if (head)
234 q->head = p->prev;
235 else
236 prev->prev = p->prev;
237 p->prev = NULL;
238 brcmu_pkt_buf_free_skb(p);
239 q->len--;
240 pq->len--;
241 p = (head ? q->head : prev->prev);
242 } else {
243 prev = p;
244 p = p->prev;
248 if (q->head == NULL)
249 q->tail = NULL;
251 EXPORT_SYMBOL(brcmu_pktq_pflush);
253 void brcmu_pktq_flush(struct pktq *pq, bool dir,
254 bool (*fn)(struct sk_buff *, void *), void *arg)
256 int prec;
257 for (prec = 0; prec < pq->num_prec; prec++)
258 brcmu_pktq_pflush(pq, prec, dir, fn, arg);
260 EXPORT_SYMBOL(brcmu_pktq_flush);
262 void brcmu_pktq_init(struct pktq *pq, int num_prec, int max_len)
264 int prec;
266 /* pq is variable size; only zero out what's requested */
267 memset(pq, 0,
268 offsetof(struct pktq, q) + (sizeof(struct pktq_prec) * num_prec));
270 pq->num_prec = (u16) num_prec;
272 pq->max = (u16) max_len;
274 for (prec = 0; prec < num_prec; prec++)
275 pq->q[prec].max = pq->max;
277 EXPORT_SYMBOL(brcmu_pktq_init);
279 struct sk_buff *brcmu_pktq_peek_tail(struct pktq *pq, int *prec_out)
281 int prec;
283 if (pq->len == 0)
284 return NULL;
286 for (prec = 0; prec < pq->hi_prec; prec++)
287 if (pq->q[prec].head)
288 break;
290 if (prec_out)
291 *prec_out = prec;
293 return pq->q[prec].tail;
295 EXPORT_SYMBOL(brcmu_pktq_peek_tail);
297 /* Return sum of lengths of a specific set of precedences */
298 int brcmu_pktq_mlen(struct pktq *pq, uint prec_bmp)
300 int prec, len;
302 len = 0;
304 for (prec = 0; prec <= pq->hi_prec; prec++)
305 if (prec_bmp & (1 << prec))
306 len += pq->q[prec].len;
308 return len;
310 EXPORT_SYMBOL(brcmu_pktq_mlen);
312 /* Priority dequeue from a specific set of precedences */
313 struct sk_buff *brcmu_pktq_mdeq(struct pktq *pq, uint prec_bmp,
314 int *prec_out)
316 struct pktq_prec *q;
317 struct sk_buff *p;
318 int prec;
320 if (pq->len == 0)
321 return NULL;
323 while ((prec = pq->hi_prec) > 0 && pq->q[prec].head == NULL)
324 pq->hi_prec--;
326 while ((prec_bmp & (1 << prec)) == 0 || pq->q[prec].head == NULL)
327 if (prec-- == 0)
328 return NULL;
330 q = &pq->q[prec];
332 p = q->head;
333 if (p == NULL)
334 return NULL;
336 q->head = p->prev;
337 if (q->head == NULL)
338 q->tail = NULL;
340 q->len--;
342 if (prec_out)
343 *prec_out = prec;
345 pq->len--;
347 p->prev = NULL;
349 return p;
351 EXPORT_SYMBOL(brcmu_pktq_mdeq);
353 #if defined(BCMDBG)
354 /* pretty hex print a pkt buffer chain */
355 void brcmu_prpkt(const char *msg, struct sk_buff *p0)
357 struct sk_buff *p;
359 if (msg && (msg[0] != '\0'))
360 printk(KERN_DEBUG "%s:\n", msg);
362 for (p = p0; p; p = p->next)
363 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, p->data, p->len);
365 EXPORT_SYMBOL(brcmu_prpkt);
366 #endif /* defined(BCMDBG) */
368 #if defined(BCMDBG)
370 * print bytes formatted as hex to a string. return the resulting
371 * string length
373 int brcmu_format_hex(char *str, const void *bytes, int len)
375 int i;
376 char *p = str;
377 const u8 *src = (const u8 *)bytes;
379 for (i = 0; i < len; i++) {
380 p += snprintf(p, 3, "%02X", *src);
381 src++;
383 return (int)(p - str);
385 EXPORT_SYMBOL(brcmu_format_hex);
386 #endif /* defined(BCMDBG) */