master
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#ifndef USB_GLOBAL_INCLUDE_FILE
32#include <sys/stdint.h>
33#include <sys/endian.h>
34#endif
35
36/*
37 * Declare the basic USB record types. USB records have an alignment
38 * of 1 byte and are always packed.
39 */
40typedef uint8_t uByte;
41typedef uint8_t uWord[2];
42typedef uint8_t uDWord[4];
43typedef uint8_t uQWord[8];
44
45/*
46 * Define a set of macros that can get and set data independent of
47 * CPU endianness and CPU alignment requirements:
48 */
49#define UGETB(w) \
50 ((w)[0])
51
52#define UGETW(w) \
53 ((w)[0] | \
54 (((uint16_t)((w)[1])) << 8))
55
56#define UGETDW(w) \
57 ((w)[0] | \
58 (((uint16_t)((w)[1])) << 8) | \
59 (((uint32_t)((w)[2])) << 16) | \
60 (((uint32_t)((w)[3])) << 24))
61
62#define UGETQW(w) \
63 ((w)[0] | \
64 (((uint16_t)((w)[1])) << 8) | \
65 (((uint32_t)((w)[2])) << 16) | \
66 (((uint32_t)((w)[3])) << 24) | \
67 (((uint64_t)((w)[4])) << 32) | \
68 (((uint64_t)((w)[5])) << 40) | \
69 (((uint64_t)((w)[6])) << 48) | \
70 (((uint64_t)((w)[7])) << 56))
71
72#define USETB(w,v) do { \
73 (w)[0] = (uint8_t)(v); \
74} while (0)
75
76#define USETW(w,v) do { \
77 (w)[0] = (uint8_t)(v); \
78 (w)[1] = (uint8_t)((v) >> 8); \
79} while (0)
80
81#define USETDW(w,v) do { \
82 (w)[0] = (uint8_t)(v); \
83 (w)[1] = (uint8_t)((v) >> 8); \
84 (w)[2] = (uint8_t)((v) >> 16); \
85 (w)[3] = (uint8_t)((v) >> 24); \
86} while (0)
87
88#define USETQW(w,v) do { \
89 (w)[0] = (uint8_t)(v); \
90 (w)[1] = (uint8_t)((v) >> 8); \
91 (w)[2] = (uint8_t)((v) >> 16); \
92 (w)[3] = (uint8_t)((v) >> 24); \
93 (w)[4] = (uint8_t)((v) >> 32); \
94 (w)[5] = (uint8_t)((v) >> 40); \
95 (w)[6] = (uint8_t)((v) >> 48); \
96 (w)[7] = (uint8_t)((v) >> 56); \
97} while (0)
98
99#define USETW2(w,b1,b0) do { \
100 (w)[0] = (uint8_t)(b0); \
101 (w)[1] = (uint8_t)(b1); \
102} while (0)
103
104#define USETW4(w,b3,b2,b1,b0) do { \
105 (w)[0] = (uint8_t)(b0); \
106 (w)[1] = (uint8_t)(b1); \
107 (w)[2] = (uint8_t)(b2); \
108 (w)[3] = (uint8_t)(b3); \
109} while (0)
110
111#define USETW8(w,b7,b6,b5,b4,b3,b2,b1,b0) do { \
112 (w)[0] = (uint8_t)(b0); \
113 (w)[1] = (uint8_t)(b1); \
114 (w)[2] = (uint8_t)(b2); \
115 (w)[3] = (uint8_t)(b3); \
116 (w)[4] = (uint8_t)(b4); \
117 (w)[5] = (uint8_t)(b5); \
118 (w)[6] = (uint8_t)(b6); \
119 (w)[7] = (uint8_t)(b7); \
120} while (0)
121
122#endif /* _USB_ENDIAN_H_ */