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