auto
     ˆÀÊ„Œ¼×¿ÂÇ ?÷     /* SPDX-License-Identifier: BSD-3-Clause
 * Copyright 2016-2018 NXP
 * Copyright (c) 2018-2019, Vladimir Oltean <olteanv@gmail.com>
 */
#ifndef _LINUX_PACKING_H
#define _LINUX_PACKING_H

#include <linux/types.h>
#include <linux/bitops.h>

#define GEN_PACKED_FIELD_STRUCT(__type) \
	struct packed_field_ ## __type { \
		__type startbit; \
		__type endbit; \
		__type offset; \
		__type size; \
	}

/* struct packed_field_u8. Use with bit offsets < 256, buffers < 32B and
 * unpacked structures < 256B.
 */
GEN_PACKED_FIELD_STRUCT(u8);

/* struct packed_field_u16. Use with bit offsets < 65536, buffers < 8KB and
 * unpacked structures < 64KB.
 */
GEN_PACKED_FIELD_STRUCT(u16);

#define PACKED_FIELD(start, end, struct_name, struct_field) \
{ \
	(start), \
	(end), \
	offsetof(struct_name, struct_field), \
	sizeof_field(struct_name, struct_field), \
}

#define CHECK_PACKED_FIELD_OVERLAP(fields, index1, index2) ({ \
	typeof(&(fields)[0]) __f = (fields); \
	typeof(__f[0]) _f1 = __f[index1]; typeof(__f[0]) _f2 = __f[index2]; \
	const bool _ascending = __f[0].startbit < __f[1].startbit; \
	BUILD_BUG_ON_MSG(_ascending && _f1.startbit >= _f2.startbit, \
			 __stringify(fields) " field " __stringify(index2) \
			 " breaks ascending order"); \
	BUILD_BUG_ON_MSG(!_ascending && _f1.startbit <= _f2.startbit, \
			 __stringify(fields) " field " __stringify(index2) \
			 " breaks descending order"); \
	BUILD_BUG_ON_MSG(max(_f1.endbit, _f2.endbit) <= \
			 min(_f1.startbit, _f2.startbit), \
			 __stringify(fields) " field " __stringify(index2) \
			 " overlaps with previous field"); \
})

#define CHECK_PACKED_FIELD(fields, index) ({ \
	typeof(&(fields)[0]) _f = (fields); \
	typeof(_f[0]) __f = _f[index]; \
	BUILD_BUG_ON_MSG(__f.startbit < __f.endbit, \
			 __stringify(fields) " field " __stringify(index) \
			 " start bit must not be smaller than end bit"); \
	BUILD_BUG_ON_MSG(__f.size != 1 && __f.size != 2 && \
			 __f.size != 4 && __f.size != 8, \
			 __stringify(fields) " field " __stringify(index) \
			" has unsupported unpacked storage size"); \
	BUILD_BUG_ON_MSG(__f.startbit - __f.endbit >= BITS_PER_BYTE * __f.size, \
			 __stringify(fields) " field " __stringify(index) \
			 " exceeds unpacked storage size"); \
	__builtin_choose_expr(index != 0, \
			      CHECK_PACKED_FIELD_OVERLAP(fields, index - 1, index), \
			      1); \
})

/* Note that the packed fields may be either in ascending or descending order.
 * Thus, we must check that both the first and last field wit within the
 * packed buffer size.
 */
#define CHECK_PACKED_FIELDS_SIZE(fields, pbuflen) ({ \
	typeof(&(fields)[0]) _f = (fields); \
	typeof(pbuflen) _len = (pbuflen); \
	const size_t num_fields = ARRAY_SIZE(fields); \
	BUILD_BUG_ON_MSG(!__builtin_constant_p(_len), \
			 __stringify(fields) " pbuflen " __stringify(pbuflen) \
			 " must be a compile time constant"); \
	BUILD_BUG_ON_MSG(_f[0].startbit >= BITS_PER_BYTE * _len, \
			 __stringify(fields) " first field exceeds packed buffer size"); \
	BUILD_BUG_ON_MSG(_f[num_fields - 1].startbit >= BITS_PER_BYTE * _len, \
			 __stringify(fields) " last field exceeds packed buffer size"); \
})

#define QUIRK_MSB_ON_THE_RIGHT	BIT(0)
#define QUIRK_LITTLE_ENDIAN	BIT(1)
#define QUIRK_LSW32_IS_FIRST	BIT(2)

enum packing_op {
	PACK,
	UNPACK,
};

int packing(void *pbuf, u64 *uval, int startbit, int endbit, size_t pbuflen,
	    enum packing_op op, u8 quirks);

int pack(void *pbuf, u64 uval, size_t startbit, size_t endbit, size_t pbuflen,
	 u8 quirks);

int unpack(const void *pbuf, u64 *uval, size_t startbit, size_t endbit,
	   size_t pbuflen, u8 quirks);

void pack_fields_u8(void *pbuf, size_t pbuflen, const void *ustruct,
		    const struct packed_field_u8 *fields, size_t num_fields,
		    u8 quirks);

void pack_fields_u16(void *pbuf, size_t pbuflen, const void *ustruct,
		     const struct packed_field_u16 *fields, size_t num_fields,
		     u8 quirks);

void unpack_fields_u8(const void *pbuf, size_t pbuflen, void *ustruct,
		      const struct packed_field_u8 *fields, size_t num_fields,
		      u8 quirks)