xref: /freebsd-src/sys/dev/usb/usb_endian.h (revision 71625ec9ad2a9bc8c09784fbd23b759830e0ee5f)
102ac6454SAndrew Thompson /*
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
402ac6454SAndrew Thompson  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
502ac6454SAndrew Thompson  *
602ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
702ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
802ac6454SAndrew Thompson  * are met:
902ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
1002ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1102ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1202ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1302ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1402ac6454SAndrew Thompson  *
1502ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1602ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1702ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1802ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1902ac6454SAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2002ac6454SAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2102ac6454SAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2202ac6454SAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2302ac6454SAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2402ac6454SAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2502ac6454SAndrew Thompson  * SUCH DAMAGE.
2602ac6454SAndrew Thompson  */
2702ac6454SAndrew Thompson 
2875973647SAndrew Thompson #ifndef _USB_ENDIAN_H_
2975973647SAndrew Thompson #define	_USB_ENDIAN_H_
3002ac6454SAndrew Thompson 
31d2b99310SHans Petter Selasky #ifndef USB_GLOBAL_INCLUDE_FILE
3202ac6454SAndrew Thompson #include <sys/stdint.h>
3302ac6454SAndrew Thompson #include <sys/endian.h>
34d2b99310SHans Petter Selasky #endif
3502ac6454SAndrew Thompson 
3602ac6454SAndrew Thompson /*
3702ac6454SAndrew Thompson  * Declare the basic USB record types. USB records have an alignment
3802ac6454SAndrew Thompson  * of 1 byte and are always packed.
3902ac6454SAndrew Thompson  */
4002ac6454SAndrew Thompson typedef uint8_t uByte;
4102ac6454SAndrew Thompson typedef uint8_t uWord[2];
4202ac6454SAndrew Thompson typedef uint8_t uDWord[4];
4302ac6454SAndrew Thompson typedef uint8_t uQWord[8];
4402ac6454SAndrew Thompson 
4502ac6454SAndrew Thompson /*
4602ac6454SAndrew Thompson  * Define a set of macros that can get and set data independent of
4702ac6454SAndrew Thompson  * CPU endianness and CPU alignment requirements:
4802ac6454SAndrew Thompson  */
4902ac6454SAndrew Thompson #define	UGETB(w)			\
5002ac6454SAndrew Thompson   ((w)[0])
5102ac6454SAndrew Thompson 
5202ac6454SAndrew Thompson #define	UGETW(w)			\
5302ac6454SAndrew Thompson   ((w)[0] |				\
54c48fb077SAndrew Thompson   (((uint16_t)((w)[1])) << 8))
5502ac6454SAndrew Thompson 
5602ac6454SAndrew Thompson #define	UGETDW(w)			\
5702ac6454SAndrew Thompson   ((w)[0] |				\
58c48fb077SAndrew Thompson   (((uint16_t)((w)[1])) << 8) |		\
59c48fb077SAndrew Thompson   (((uint32_t)((w)[2])) << 16) |	\
60c48fb077SAndrew Thompson   (((uint32_t)((w)[3])) << 24))
6102ac6454SAndrew Thompson 
6202ac6454SAndrew Thompson #define	UGETQW(w)			\
6302ac6454SAndrew Thompson   ((w)[0] |				\
64c48fb077SAndrew Thompson   (((uint16_t)((w)[1])) << 8) |		\
65c48fb077SAndrew Thompson   (((uint32_t)((w)[2])) << 16) |	\
66c48fb077SAndrew Thompson   (((uint32_t)((w)[3])) << 24) |	\
6702ac6454SAndrew Thompson   (((uint64_t)((w)[4])) << 32) |	\
6802ac6454SAndrew Thompson   (((uint64_t)((w)[5])) << 40) |	\
6902ac6454SAndrew Thompson   (((uint64_t)((w)[6])) << 48) |	\
7002ac6454SAndrew Thompson   (((uint64_t)((w)[7])) << 56))
7102ac6454SAndrew Thompson 
7202ac6454SAndrew Thompson #define	USETB(w,v) do {			\
7302ac6454SAndrew Thompson   (w)[0] = (uint8_t)(v);		\
7402ac6454SAndrew Thompson } while (0)
7502ac6454SAndrew Thompson 
7602ac6454SAndrew Thompson #define	USETW(w,v) do {			\
7702ac6454SAndrew Thompson   (w)[0] = (uint8_t)(v);		\
7802ac6454SAndrew Thompson   (w)[1] = (uint8_t)((v) >> 8);		\
7902ac6454SAndrew Thompson } while (0)
8002ac6454SAndrew Thompson 
8102ac6454SAndrew Thompson #define	USETDW(w,v) do {		\
8202ac6454SAndrew Thompson   (w)[0] = (uint8_t)(v);		\
8302ac6454SAndrew Thompson   (w)[1] = (uint8_t)((v) >> 8);		\
8402ac6454SAndrew Thompson   (w)[2] = (uint8_t)((v) >> 16);	\
8502ac6454SAndrew Thompson   (w)[3] = (uint8_t)((v) >> 24);	\
8602ac6454SAndrew Thompson } while (0)
8702ac6454SAndrew Thompson 
8802ac6454SAndrew Thompson #define	USETQW(w,v) do {		\
8902ac6454SAndrew Thompson   (w)[0] = (uint8_t)(v);		\
9002ac6454SAndrew Thompson   (w)[1] = (uint8_t)((v) >> 8);		\
9102ac6454SAndrew Thompson   (w)[2] = (uint8_t)((v) >> 16);	\
9202ac6454SAndrew Thompson   (w)[3] = (uint8_t)((v) >> 24);	\
9302ac6454SAndrew Thompson   (w)[4] = (uint8_t)((v) >> 32);	\
9402ac6454SAndrew Thompson   (w)[5] = (uint8_t)((v) >> 40);	\
9502ac6454SAndrew Thompson   (w)[6] = (uint8_t)((v) >> 48);	\
9602ac6454SAndrew Thompson   (w)[7] = (uint8_t)((v) >> 56);	\
9702ac6454SAndrew Thompson } while (0)
9802ac6454SAndrew Thompson 
9902ac6454SAndrew Thompson #define	USETW2(w,b1,b0) do {		\
10002ac6454SAndrew Thompson   (w)[0] = (uint8_t)(b0);		\
10102ac6454SAndrew Thompson   (w)[1] = (uint8_t)(b1);		\
10202ac6454SAndrew Thompson } while (0)
10302ac6454SAndrew Thompson 
10402ac6454SAndrew Thompson #define	USETW4(w,b3,b2,b1,b0) do {	\
10502ac6454SAndrew Thompson   (w)[0] = (uint8_t)(b0);		\
10602ac6454SAndrew Thompson   (w)[1] = (uint8_t)(b1);		\
10702ac6454SAndrew Thompson   (w)[2] = (uint8_t)(b2);		\
10802ac6454SAndrew Thompson   (w)[3] = (uint8_t)(b3);		\
10902ac6454SAndrew Thompson } while (0)
11002ac6454SAndrew Thompson 
11102ac6454SAndrew Thompson #define	USETW8(w,b7,b6,b5,b4,b3,b2,b1,b0) do {	\
11202ac6454SAndrew Thompson   (w)[0] = (uint8_t)(b0);		\
11302ac6454SAndrew Thompson   (w)[1] = (uint8_t)(b1);		\
11402ac6454SAndrew Thompson   (w)[2] = (uint8_t)(b2);		\
11502ac6454SAndrew Thompson   (w)[3] = (uint8_t)(b3);		\
11602ac6454SAndrew Thompson   (w)[4] = (uint8_t)(b4);		\
11702ac6454SAndrew Thompson   (w)[5] = (uint8_t)(b5);		\
11802ac6454SAndrew Thompson   (w)[6] = (uint8_t)(b6);		\
11902ac6454SAndrew Thompson   (w)[7] = (uint8_t)(b7);		\
12002ac6454SAndrew Thompson } while (0)
12102ac6454SAndrew Thompson 
12275973647SAndrew Thompson #endif					/* _USB_ENDIAN_H_ */
123