1 // SPDX-License-Identifier: ISC
3 * Copyright (c) 2019 Broadcom
6 #include <asm/unaligned.h>
8 #include <linux/math.h>
9 #include <linux/string.h>
10 #include <linux/bug.h>
14 static int brcmf_xtlv_header_size(u16 opts)
16 int len = (int)offsetof(struct brcmf_xtlv, data);
18 if (opts & BRCMF_XTLV_OPTION_IDU8)
20 if (opts & BRCMF_XTLV_OPTION_LENU8)
26 int brcmf_xtlv_data_size(int dlen, u16 opts)
30 hsz = brcmf_xtlv_header_size(opts);
31 if (opts & BRCMF_XTLV_OPTION_ALIGN32)
32 return roundup(dlen + hsz, 4);
37 void brcmf_xtlv_pack_header(struct brcmf_xtlv *xtlv, u16 id, u16 len,
38 const u8 *data, u16 opts)
41 u16 mask = BRCMF_XTLV_OPTION_IDU8 | BRCMF_XTLV_OPTION_LENU8;
45 u8 *lenp = idp + sizeof(xtlv->id);
47 put_unaligned_le16(id, idp);
48 put_unaligned_le16(len, lenp);
49 data_buf = lenp + sizeof(u16);
50 } else if ((opts & mask) == mask) { /* u8 id and u8 len */
56 data_buf = lenp + sizeof(u8);
57 } else if (opts & BRCMF_XTLV_OPTION_IDU8) { /* u8 id, u16 len */
62 put_unaligned_le16(len, lenp);
63 data_buf = lenp + sizeof(u16);
64 } else if (opts & BRCMF_XTLV_OPTION_LENU8) { /* u16 id, u8 len */
66 u8 *lenp = idp + sizeof(u16);
68 put_unaligned_le16(id, idp);
70 data_buf = lenp + sizeof(u8);
72 WARN(true, "Unexpected xtlv option");
76 if (opts & BRCMF_XTLV_OPTION_LENU8) {
77 WARN_ON(len > 0x00ff);
82 memcpy(data_buf, data, len);