1 // SPDX-License-Identifier: ISC
3 * Copyright (c) 2019 Broadcom
6 #include <asm/unaligned.h>
7 #include <linux/string.h>
12 static int brcmf_xtlv_header_size(u16 opts)
14 int len = (int)offsetof(struct brcmf_xtlv, data);
16 if (opts & BRCMF_XTLV_OPTION_IDU8)
18 if (opts & BRCMF_XTLV_OPTION_LENU8)
24 int brcmf_xtlv_data_size(int dlen, u16 opts)
28 hsz = brcmf_xtlv_header_size(opts);
29 if (opts & BRCMF_XTLV_OPTION_ALIGN32)
30 return roundup(dlen + hsz, 4);
35 void brcmf_xtlv_pack_header(struct brcmf_xtlv *xtlv, u16 id, u16 len,
36 const u8 *data, u16 opts)
39 u16 mask = BRCMF_XTLV_OPTION_IDU8 | BRCMF_XTLV_OPTION_LENU8;
43 u8 *lenp = idp + sizeof(xtlv->id);
45 put_unaligned_le16(id, idp);
46 put_unaligned_le16(len, lenp);
47 data_buf = lenp + sizeof(u16);
48 } else if ((opts & mask) == mask) { /* u8 id and u8 len */
54 data_buf = lenp + sizeof(u8);
55 } else if (opts & BRCMF_XTLV_OPTION_IDU8) { /* u8 id, u16 len */
60 put_unaligned_le16(len, lenp);
61 data_buf = lenp + sizeof(u16);
62 } else if (opts & BRCMF_XTLV_OPTION_LENU8) { /* u16 id, u8 len */
64 u8 *lenp = idp + sizeof(u16);
66 put_unaligned_le16(id, idp);
68 data_buf = lenp + sizeof(u8);
70 WARN(true, "Unexpected xtlv option");
74 if (opts & BRCMF_XTLV_OPTION_LENU8) {
75 WARN_ON(len > 0x00ff);
80 memcpy(data_buf, data, len);