linux/drivers/net/wireless/broadcom/brcm80211/brcmfmac/xtlv.c
<<
>>
Prefs
   1// SPDX-License-Identifier: ISC
   2/*
   3 * Copyright (c) 2019 Broadcom
   4 */
   5
   6#include <asm/unaligned.h>
   7#include <linux/string.h>
   8#include <linux/bug.h>
   9
  10#include "xtlv.h"
  11
  12static int brcmf_xtlv_header_size(u16 opts)
  13{
  14        int len = (int)offsetof(struct brcmf_xtlv, data);
  15
  16        if (opts & BRCMF_XTLV_OPTION_IDU8)
  17                --len;
  18        if (opts & BRCMF_XTLV_OPTION_LENU8)
  19                --len;
  20
  21        return len;
  22}
  23
  24int brcmf_xtlv_data_size(int dlen, u16 opts)
  25{
  26        int hsz;
  27
  28        hsz = brcmf_xtlv_header_size(opts);
  29        if (opts & BRCMF_XTLV_OPTION_ALIGN32)
  30                return roundup(dlen + hsz, 4);
  31
  32        return dlen + hsz;
  33}
  34
  35void brcmf_xtlv_pack_header(struct brcmf_xtlv *xtlv, u16 id, u16 len,
  36                            const u8 *data, u16 opts)
  37{
  38        u8 *data_buf;
  39        u16 mask = BRCMF_XTLV_OPTION_IDU8 | BRCMF_XTLV_OPTION_LENU8;
  40
  41        if (!(opts & mask)) {
  42                u8 *idp = (u8 *)xtlv;
  43                u8 *lenp = idp + sizeof(xtlv->id);
  44
  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 */
  49                u8 *idp = (u8 *)xtlv;
  50                u8 *lenp = idp + 1;
  51
  52                *idp = (u8)id;
  53                *lenp = (u8)len;
  54                data_buf = lenp + sizeof(u8);
  55        } else if (opts & BRCMF_XTLV_OPTION_IDU8) { /* u8 id, u16 len */
  56                u8 *idp = (u8 *)xtlv;
  57                u8 *lenp = idp + 1;
  58
  59                *idp = (u8)id;
  60                put_unaligned_le16(len, lenp);
  61                data_buf = lenp + sizeof(u16);
  62        } else if (opts & BRCMF_XTLV_OPTION_LENU8) { /* u16 id, u8 len */
  63                u8 *idp = (u8 *)xtlv;
  64                u8 *lenp = idp + sizeof(u16);
  65
  66                put_unaligned_le16(id, idp);
  67                *lenp = (u8)len;
  68                data_buf = lenp + sizeof(u8);
  69        } else {
  70                WARN(true, "Unexpected xtlv option");
  71                return;
  72        }
  73
  74        if (opts & BRCMF_XTLV_OPTION_LENU8) {
  75                WARN_ON(len > 0x00ff);
  76                len &= 0xff;
  77        }
  78
  79        if (data)
  80                memcpy(data_buf, data, len);
  81}
  82
  83