xref: /freebsd/sys/dev/usb/usb_endian.h (revision eb69d1f144a6fcc765d1b9d44a5ae8082353e70b)
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 #ifndef USB_GLOBAL_INCLUDE_FILE
33 #include <sys/stdint.h>
34 #include <sys/endian.h>
35 #endif
36 
37 /*
38  * Declare the basic USB record types. USB records have an alignment
39  * of 1 byte and are always packed.
40  */
41 typedef uint8_t uByte;
42 typedef uint8_t uWord[2];
43 typedef uint8_t uDWord[4];
44 typedef uint8_t uQWord[8];
45 
46 /*
47  * Define a set of macros that can get and set data independent of
48  * CPU endianness and CPU alignment requirements:
49  */
50 #define	UGETB(w)			\
51   ((w)[0])
52 
53 #define	UGETW(w)			\
54   ((w)[0] |				\
55   (((uint16_t)((w)[1])) << 8))
56 
57 #define	UGETDW(w)			\
58   ((w)[0] |				\
59   (((uint16_t)((w)[1])) << 8) |		\
60   (((uint32_t)((w)[2])) << 16) |	\
61   (((uint32_t)((w)[3])) << 24))
62 
63 #define	UGETQW(w)			\
64   ((w)[0] |				\
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))
72 
73 #define	USETB(w,v) do {			\
74   (w)[0] = (uint8_t)(v);		\
75 } while (0)
76 
77 #define	USETW(w,v) do {			\
78   (w)[0] = (uint8_t)(v);		\
79   (w)[1] = (uint8_t)((v) >> 8);		\
80 } while (0)
81 
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);	\
87 } while (0)
88 
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);	\
98 } while (0)
99 
100 #define	USETW2(w,b1,b0) do {		\
101   (w)[0] = (uint8_t)(b0);		\
102   (w)[1] = (uint8_t)(b1);		\
103 } while (0)
104 
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);		\
110 } while (0)
111 
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);		\
121 } while (0)
122 
123 #endif					/* _USB_ENDIAN_H_ */
124