xref: /third_party/FreeBSD/sys/dev/usb/usb_endian.h (revision f9f848fa)
1/*
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28#ifndef _USB_ENDIAN_H_
29#define	_USB_ENDIAN_H_
30
31/*
32 * Declare the basic USB record types. USB records have an alignment
33 * of 1 byte and are always packed.
34 */
35typedef uint8_t uByte;
36typedef uint8_t uWord[2];
37typedef uint8_t uDWord[4];
38typedef uint8_t uQWord[8];
39
40/*
41 * Define a set of macros that can get and set data independent of
42 * CPU endianness and CPU alignment requirements:
43 */
44#define	UGETB(w)			\
45  ((w)[0])
46
47#define	UGETW(w)			\
48  ((w)[0] |				\
49  (((uint16_t)((w)[1])) << 8))
50
51#define	UGETDW(w)			\
52  ((w)[0] |				\
53  (((uint16_t)((w)[1])) << 8) |		\
54  (((uint32_t)((w)[2])) << 16) |	\
55  (((uint32_t)((w)[3])) << 24))
56
57#define	UGETQW(w)			\
58  ((w)[0] |				\
59  (((uint16_t)((w)[1])) << 8) |		\
60  (((uint32_t)((w)[2])) << 16) |	\
61  (((uint32_t)((w)[3])) << 24) |	\
62  (((uint64_t)((w)[4])) << 32) |	\
63  (((uint64_t)((w)[5])) << 40) |	\
64  (((uint64_t)((w)[6])) << 48) |	\
65  (((uint64_t)((w)[7])) << 56))
66
67#define	USETB(w,v) do {			\
68  (w)[0] = (uint8_t)(v);		\
69} while (0)
70
71#define	USETW(w,v) do {			\
72  (w)[0] = (uint8_t)(v);		\
73  (w)[1] = (uint8_t)((v) >> 8);		\
74} while (0)
75
76#define	USETDW(w,v) do {		\
77  (w)[0] = (uint8_t)(v);		\
78  (w)[1] = (uint8_t)((v) >> 8);		\
79  (w)[2] = (uint8_t)((v) >> 16);	\
80  (w)[3] = (uint8_t)((v) >> 24);	\
81} while (0)
82
83#define	USETQW(w,v) do {		\
84  (w)[0] = (uint8_t)(v);		\
85  (w)[1] = (uint8_t)((v) >> 8);		\
86  (w)[2] = (uint8_t)((v) >> 16);	\
87  (w)[3] = (uint8_t)((v) >> 24);	\
88  (w)[4] = (uint8_t)((v) >> 32);	\
89  (w)[5] = (uint8_t)((v) >> 40);	\
90  (w)[6] = (uint8_t)((v) >> 48);	\
91  (w)[7] = (uint8_t)((v) >> 56);	\
92} while (0)
93
94#define	USETW2(w,b1,b0) do {		\
95  (w)[0] = (uint8_t)(b0);		\
96  (w)[1] = (uint8_t)(b1);		\
97} while (0)
98
99#define	USETW4(w,b3,b2,b1,b0) do {	\
100  (w)[0] = (uint8_t)(b0);		\
101  (w)[1] = (uint8_t)(b1);		\
102  (w)[2] = (uint8_t)(b2);		\
103  (w)[3] = (uint8_t)(b3);		\
104} while (0)
105
106#define	USETW8(w,b7,b6,b5,b4,b3,b2,b1,b0) do {	\
107  (w)[0] = (uint8_t)(b0);		\
108  (w)[1] = (uint8_t)(b1);		\
109  (w)[2] = (uint8_t)(b2);		\
110  (w)[3] = (uint8_t)(b3);		\
111  (w)[4] = (uint8_t)(b4);		\
112  (w)[5] = (uint8_t)(b5);		\
113  (w)[6] = (uint8_t)(b6);		\
114  (w)[7] = (uint8_t)(b7);		\
115} while (0)
116
117#endif					/* _USB_ENDIAN_H_ */
118