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