]> mj.ucw.cz Git - pciutils.git/blob - lib/sysdep.h
lspci: Decode PCIe DevCtl2 ID-Based Ordering Enables
[pciutils.git] / lib / sysdep.h
1 /*
2  *      The PCI Library -- System-Dependent Stuff
3  *
4  *      Copyright (c) 1997--2020 Martin Mares <mj@ucw.cz>
5  *
6  *      Can be freely distributed and used under the terms of the GNU GPL v2+
7  *
8  *      SPDX-License-Identifier: GPL-2.0-or-later
9  */
10
11 #ifdef __GNUC__
12 #define UNUSED __attribute__((unused))
13 #define NONRET __attribute__((noreturn))
14 #define FORMAT_CHECK(x,y,z) __attribute__((format(x,y,z)))
15 #else
16 #define UNUSED
17 #define NONRET
18 #define FORMAT_CHECK(x,y,z)
19 #define inline
20 #endif
21
22 typedef u8 byte;
23 typedef u16 word;
24
25 #ifdef PCI_OS_WINDOWS
26 #define strcasecmp _strcmpi
27 #define strncasecmp _strnicmp
28 #if defined(_MSC_VER) && _MSC_VER < 1900
29 #define snprintf _snprintf
30 #define vsnprintf _vsnprintf
31 #endif
32 #endif
33
34 #ifdef PCI_HAVE_LINUX_BYTEORDER_H
35
36 #include <asm/byteorder.h>
37 #define cpu_to_le16 __cpu_to_le16
38 #define cpu_to_le32 __cpu_to_le32
39 #define le16_to_cpu __le16_to_cpu
40 #define le32_to_cpu __le32_to_cpu
41
42 #else
43
44 #ifdef PCI_OS_LINUX
45 #include <endian.h>
46 #define BYTE_ORDER __BYTE_ORDER
47 #define BIG_ENDIAN __BIG_ENDIAN
48 #endif
49
50 #ifdef PCI_OS_SUNOS
51 #include <sys/byteorder.h>
52 #if defined(__i386) && defined(LITTLE_ENDIAN)
53 # define BYTE_ORDER LITTLE_ENDIAN
54 #elif defined(__sparc) && defined(BIG_ENDIAN)
55 # define BYTE_ORDER BIG_ENDIAN
56 #else
57 #define BIG_ENDIAN 4321
58 #endif
59 #ifndef BYTE_ORDER
60 #ifdef _LITTLE_ENDIAN
61 #define BYTE_ORDER 1234
62 #else
63 #define BYTE_ORDER 4321
64 #endif
65 #endif /* BYTE_ORDER */
66 #endif /* PCI_OS_SUNOS */
67
68 #ifdef PCI_OS_WINDOWS
69 #ifdef __MINGW32__
70   #include <sys/param.h>
71 #else
72   #include <io.h>
73   #define BIG_ENDIAN 4321
74   #define LITTLE_ENDIAN 1234
75   #define BYTE_ORDER LITTLE_ENDIAN
76 #endif
77 #endif
78
79 #ifdef PCI_OS_SYLIXOS
80 #include <endian.h>
81 #endif
82
83 #ifdef PCI_OS_DJGPP
84   #define BIG_ENDIAN 4321
85   #define LITTLE_ENDIAN 1234
86   #define BYTE_ORDER LITTLE_ENDIAN
87 #endif
88
89 #if !defined(BYTE_ORDER)
90 #error "BYTE_ORDER not defined for your platform"
91 #endif
92
93 #if BYTE_ORDER == BIG_ENDIAN
94 #define cpu_to_le16 swab16
95 #define cpu_to_le32 swab32
96 #define le16_to_cpu swab16
97 #define le32_to_cpu swab32
98
99 static inline word swab16(word w)
100 {
101   return (w << 8) | ((w >> 8) & 0xff);
102 }
103
104 static inline u32 swab32(u32 w)
105 {
106   return ((w & 0xff000000) >> 24) |
107          ((w & 0x00ff0000) >> 8) |
108          ((w & 0x0000ff00) << 8)  |
109          ((w & 0x000000ff) << 24);
110 }
111 #else
112 #define cpu_to_le16(x) (x)
113 #define cpu_to_le32(x) (x)
114 #define le16_to_cpu(x) (x)
115 #define le32_to_cpu(x) (x)
116 #endif
117
118 #endif