• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* $FreeBSD$ */
2 /*
3  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4  *
5  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28 
29 #ifndef _USB_ENDIAN_H_
30 #define	_USB_ENDIAN_H_
31 
32 /*
33  * Declare the basic USB record types. USB records have an alignment
34  * of 1 byte and are always packed.
35  */
36 typedef uint8_t uByte;
37 typedef uint8_t uWord[2];
38 typedef uint8_t uDWord[4];
39 typedef uint8_t uQWord[8];
40 
41 /*
42  * Define a set of macros that can get and set data independent of
43  * CPU endianness and CPU alignment requirements:
44  */
45 #define	UGETB(w)			\
46   ((w)[0])
47 
48 #define	UGETW(w)			\
49   ((w)[0] |				\
50   (((uint16_t)((w)[1])) << 8))
51 
52 #define	UGETDW(w)			\
53   ((w)[0] |				\
54   (((uint16_t)((w)[1])) << 8) |		\
55   (((uint32_t)((w)[2])) << 16) |	\
56   (((uint32_t)((w)[3])) << 24))
57 
58 #define	UGETQW(w)			\
59   ((w)[0] |				\
60   (((uint16_t)((w)[1])) << 8) |		\
61   (((uint32_t)((w)[2])) << 16) |	\
62   (((uint32_t)((w)[3])) << 24) |	\
63   (((uint64_t)((w)[4])) << 32) |	\
64   (((uint64_t)((w)[5])) << 40) |	\
65   (((uint64_t)((w)[6])) << 48) |	\
66   (((uint64_t)((w)[7])) << 56))
67 
68 #define	USETB(w,v) do {			\
69   (w)[0] = (uint8_t)(v);		\
70 } while (0)
71 
72 #define	USETW(w,v) do {			\
73   (w)[0] = (uint8_t)(v);		\
74   (w)[1] = (uint8_t)((v) >> 8);		\
75 } while (0)
76 
77 #define	USETDW(w,v) do {		\
78   (w)[0] = (uint8_t)(v);		\
79   (w)[1] = (uint8_t)((v) >> 8);		\
80   (w)[2] = (uint8_t)((v) >> 16);	\
81   (w)[3] = (uint8_t)((v) >> 24);	\
82 } while (0)
83 
84 #define	USETQW(w,v) do {		\
85   (w)[0] = (uint8_t)(v);		\
86   (w)[1] = (uint8_t)((v) >> 8);		\
87   (w)[2] = (uint8_t)((v) >> 16);	\
88   (w)[3] = (uint8_t)((v) >> 24);	\
89   (w)[4] = (uint8_t)((v) >> 32);	\
90   (w)[5] = (uint8_t)((v) >> 40);	\
91   (w)[6] = (uint8_t)((v) >> 48);	\
92   (w)[7] = (uint8_t)((v) >> 56);	\
93 } while (0)
94 
95 #define	USETW2(w,b1,b0) do {		\
96   (w)[0] = (uint8_t)(b0);		\
97   (w)[1] = (uint8_t)(b1);		\
98 } while (0)
99 
100 #define	USETW4(w,b3,b2,b1,b0) do {	\
101   (w)[0] = (uint8_t)(b0);		\
102   (w)[1] = (uint8_t)(b1);		\
103   (w)[2] = (uint8_t)(b2);		\
104   (w)[3] = (uint8_t)(b3);		\
105 } while (0)
106 
107 #define	USETW8(w,b7,b6,b5,b4,b3,b2,b1,b0) do {	\
108   (w)[0] = (uint8_t)(b0);		\
109   (w)[1] = (uint8_t)(b1);		\
110   (w)[2] = (uint8_t)(b2);		\
111   (w)[3] = (uint8_t)(b3);		\
112   (w)[4] = (uint8_t)(b4);		\
113   (w)[5] = (uint8_t)(b5);		\
114   (w)[6] = (uint8_t)(b6);		\
115   (w)[7] = (uint8_t)(b7);		\
116 } while (0)
117 
118 #endif					/* _USB_ENDIAN_H_ */
119