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 <brcmu_utils.h>
20 MODULE_AUTHOR("Broadcom Corporation");
21 MODULE_DESCRIPTION("Broadcom 802.11n wireless LAN driver utilities.");
22 MODULE_SUPPORTED_DEVICE("Broadcom 802.11n WLAN cards");
23 MODULE_LICENSE("Dual BSD/GPL");
25 struct sk_buff *brcmu_pkt_buf_get_skb(uint len)
29 skb = dev_alloc_skb(len);
37 EXPORT_SYMBOL(brcmu_pkt_buf_get_skb);
39 /* Free the driver packet. Free the tag if present */
40 void brcmu_pkt_buf_free_skb(struct sk_buff *skb)
45 /* perversion: we use skb->next to chain multi-skb packets */
51 /* cannot kfree_skb() on hard IRQ (net/core/skbuff.c) if
54 dev_kfree_skb_any(skb);
56 /* can free immediately (even in_irq()) if destructor
65 EXPORT_SYMBOL(brcmu_pkt_buf_free_skb);
68 /* copy a buffer into a pkt buffer chain */
69 uint brcmu_pktfrombuf(struct sk_buff *p, uint offset, int len,
74 /* skip 'offset' bytes */
75 for (; p && offset; p = p->next) {
76 if (offset < (uint) (p->len))
85 for (; p && len; p = p->next) {
86 n = min((uint) (p->len) - offset, (uint) len);
87 memcpy(p->data + offset, buf, n);
96 EXPORT_SYMBOL(brcmu_pktfrombuf);
98 /* return total length of buffer chain */
99 uint brcmu_pkttotlen(struct sk_buff *p)
104 for (; p; p = p->next)
108 EXPORT_SYMBOL(brcmu_pkttotlen);
111 * osl multiple-precedence packet queue
112 * hi_prec is always >= the number of the highest non-empty precedence
114 struct sk_buff *brcmu_pktq_penq(struct pktq *pq, int prec,
119 if (pktq_full(pq) || pktq_pfull(pq, prec))
134 if (pq->hi_prec < prec)
135 pq->hi_prec = (u8) prec;
139 EXPORT_SYMBOL(brcmu_pktq_penq);
141 struct sk_buff *brcmu_pktq_penq_head(struct pktq *pq, int prec,
146 if (pktq_full(pq) || pktq_pfull(pq, prec))
160 if (pq->hi_prec < prec)
161 pq->hi_prec = (u8) prec;
165 EXPORT_SYMBOL(brcmu_pktq_penq_head);
167 struct sk_buff *brcmu_pktq_pdeq(struct pktq *pq, int prec)
190 EXPORT_SYMBOL(brcmu_pktq_pdeq);
192 struct sk_buff *brcmu_pktq_pdeq_tail(struct pktq *pq, int prec)
195 struct sk_buff *p, *prev;
203 for (prev = NULL; p != q->tail; p = p->prev)
218 EXPORT_SYMBOL(brcmu_pktq_pdeq_tail);
221 brcmu_pktq_pflush(struct pktq *pq, int prec, bool dir,
222 bool (*fn)(struct sk_buff *, void *), void *arg)
225 struct sk_buff *p, *prev = NULL;
230 if (fn == NULL || (*fn) (p, arg)) {
231 bool head = (p == q->head);
235 prev->prev = p->prev;
237 brcmu_pkt_buf_free_skb(p);
240 p = (head ? q->head : prev->prev);
250 EXPORT_SYMBOL(brcmu_pktq_pflush);
252 void brcmu_pktq_flush(struct pktq *pq, bool dir,
253 bool (*fn)(struct sk_buff *, void *), void *arg)
256 for (prec = 0; prec < pq->num_prec; prec++)
257 brcmu_pktq_pflush(pq, prec, dir, fn, arg);
259 EXPORT_SYMBOL(brcmu_pktq_flush);
261 void brcmu_pktq_init(struct pktq *pq, int num_prec, int max_len)
265 /* pq is variable size; only zero out what's requested */
267 offsetof(struct pktq, q) + (sizeof(struct pktq_prec) * num_prec));
269 pq->num_prec = (u16) num_prec;
271 pq->max = (u16) max_len;
273 for (prec = 0; prec < num_prec; prec++)
274 pq->q[prec].max = pq->max;
276 EXPORT_SYMBOL(brcmu_pktq_init);
278 struct sk_buff *brcmu_pktq_peek_tail(struct pktq *pq, int *prec_out)
285 for (prec = 0; prec < pq->hi_prec; prec++)
286 if (pq->q[prec].head)
292 return pq->q[prec].tail;
294 EXPORT_SYMBOL(brcmu_pktq_peek_tail);
296 /* Return sum of lengths of a specific set of precedences */
297 int brcmu_pktq_mlen(struct pktq *pq, uint prec_bmp)
303 for (prec = 0; prec <= pq->hi_prec; prec++)
304 if (prec_bmp & (1 << prec))
305 len += pq->q[prec].len;
309 EXPORT_SYMBOL(brcmu_pktq_mlen);
311 /* Priority dequeue from a specific set of precedences */
312 struct sk_buff *brcmu_pktq_mdeq(struct pktq *pq, uint prec_bmp,
322 while ((prec = pq->hi_prec) > 0 && pq->q[prec].head == NULL)
325 while ((prec_bmp & (1 << prec)) == 0 || pq->q[prec].head == NULL)
350 EXPORT_SYMBOL(brcmu_pktq_mdeq);
353 /* pretty hex print a pkt buffer chain */
354 void brcmu_prpkt(const char *msg, struct sk_buff *p0)
358 if (msg && (msg[0] != '\0'))
359 printk(KERN_DEBUG "%s:\n", msg);
361 for (p = p0; p; p = p->next)
362 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, p->data, p->len);
364 EXPORT_SYMBOL(brcmu_prpkt);
365 #endif /* defined(BCMDBG) */
368 * Traverse a string of 1-byte tag/1-byte length/variable-length value
369 * triples, returning a pointer to the substring whose first element
372 struct brcmu_tlv *brcmu_parse_tlvs(void *buf, int buflen, uint key)
374 struct brcmu_tlv *elt;
377 elt = (struct brcmu_tlv *) buf;
380 /* find tagged parameter */
381 while (totlen >= 2) {
384 /* validate remaining totlen */
385 if ((elt->id == key) && (totlen >= (len + 2)))
388 elt = (struct brcmu_tlv *) ((u8 *) elt + (len + 2));
394 EXPORT_SYMBOL(brcmu_parse_tlvs);
399 brcmu_format_flags(const struct brcmu_bit_desc *bd, u32 flags, char *buf,
405 int slen = 0, nlen = 0;
414 for (i = 0; flags != 0; i++) {
417 if (bit == 0 && flags != 0) {
418 /* print any unnamed bits */
419 snprintf(hexstr, 16, "0x%X", flags);
421 flags = 0; /* exit loop */
422 } else if ((flags & bit) == 0)
427 /* count btwn flag space */
430 /* need NULL char as well */
433 /* copy NULL char but don't count it */
434 strncpy(p, name, nlen + 1);
436 /* copy btwn flag space and NULL char */
438 p += snprintf(p, 2, " ");
442 /* indicate the str was too short */
445 p -= 2 - len; /* overwrite last char */
446 p += snprintf(p, 2, ">");
449 return (int)(p - buf);
451 EXPORT_SYMBOL(brcmu_format_flags);
454 * print bytes formatted as hex to a string. return the resulting
457 int brcmu_format_hex(char *str, const void *bytes, int len)
461 const u8 *src = (const u8 *)bytes;
463 for (i = 0; i < len; i++) {
464 p += snprintf(p, 3, "%02X", *src);
467 return (int)(p - str);
469 EXPORT_SYMBOL(brcmu_format_hex);
470 #endif /* defined(BCMDBG) */
472 char *brcmu_chipname(uint chipid, char *buf, uint len)
476 fmt = ((chipid > 0xa000) || (chipid < 0x4000)) ? "%d" : "%x";
477 snprintf(buf, len, fmt, chipid);
480 EXPORT_SYMBOL(brcmu_chipname);
482 uint brcmu_mkiovar(char *name, char *data, uint datalen, char *buf, uint buflen)
486 len = strlen(name) + 1;
488 if ((len + datalen) > buflen)
491 strncpy(buf, name, buflen);
493 /* append data onto the end of the name string */
494 memcpy(&buf[len], data, datalen);
499 EXPORT_SYMBOL(brcmu_mkiovar);