Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: BSD-3-Clause
0002  * Copyright 2016-2018 NXP
0003  * Copyright (c) 2018-2019, Vladimir Oltean <olteanv@gmail.com>
0004  */
0005 #ifndef _LINUX_PACKING_H
0006 #define _LINUX_PACKING_H
0007 
0008 #include <linux/types.h>
0009 #include <linux/bitops.h>
0010 
0011 #define QUIRK_MSB_ON_THE_RIGHT  BIT(0)
0012 #define QUIRK_LITTLE_ENDIAN BIT(1)
0013 #define QUIRK_LSW32_IS_FIRST    BIT(2)
0014 
0015 enum packing_op {
0016     PACK,
0017     UNPACK,
0018 };
0019 
0020 /**
0021  * packing - Convert numbers (currently u64) between a packed and an unpacked
0022  *       format. Unpacked means laid out in memory in the CPU's native
0023  *       understanding of integers, while packed means anything else that
0024  *       requires translation.
0025  *
0026  * @pbuf: Pointer to a buffer holding the packed value.
0027  * @uval: Pointer to an u64 holding the unpacked value.
0028  * @startbit: The index (in logical notation, compensated for quirks) where
0029  *        the packed value starts within pbuf. Must be larger than, or
0030  *        equal to, endbit.
0031  * @endbit: The index (in logical notation, compensated for quirks) where
0032  *      the packed value ends within pbuf. Must be smaller than, or equal
0033  *      to, startbit.
0034  * @op: If PACK, then uval will be treated as const pointer and copied (packed)
0035  *  into pbuf, between startbit and endbit.
0036  *  If UNPACK, then pbuf will be treated as const pointer and the logical
0037  *  value between startbit and endbit will be copied (unpacked) to uval.
0038  * @quirks: A bit mask of QUIRK_LITTLE_ENDIAN, QUIRK_LSW32_IS_FIRST and
0039  *      QUIRK_MSB_ON_THE_RIGHT.
0040  *
0041  * Return: 0 on success, EINVAL or ERANGE if called incorrectly. Assuming
0042  *     correct usage, return code may be discarded.
0043  *     If op is PACK, pbuf is modified.
0044  *     If op is UNPACK, uval is modified.
0045  */
0046 int packing(void *pbuf, u64 *uval, int startbit, int endbit, size_t pbuflen,
0047         enum packing_op op, u8 quirks);
0048 
0049 #endif