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