xref: /OK3568_Linux_fs/buildroot/package/android-tools/0006-fix-big-endian-build.patch (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1Fix build on big endian systems
2
3The usb_linux_client.c file defines cpu_to_le16/32 by using the C
4library htole16/32 function calls. However, cpu_to_le16/32 are used
5when initializing structures, i.e in a context where a function call
6is not allowed.
7
8It works fine on little endian systems because htole16/32 are defined
9by the C library as no-ops. But on big-endian systems, they are
10actually doing something, which might involve calling a function,
11causing build failures.
12
13To solve this, we simply open-code cpu_to_le16/32 in a way that allows
14them to be used when initializing structures.
15
16Signed-off-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
17
18Index: b/core/adb/usb_linux_client.c
19===================================================================
20--- a/core/adb/usb_linux_client.c
21+++ b/core/adb/usb_linux_client.c
22@@ -34,8 +34,15 @@
23 #define MAX_PACKET_SIZE_FS	64
24 #define MAX_PACKET_SIZE_HS	512
25
26-#define cpu_to_le16(x)  htole16(x)
27-#define cpu_to_le32(x)  htole32(x)
28+#if __BYTE_ORDER == __LITTLE_ENDIAN
29+# define cpu_to_le16(x) (x)
30+# define cpu_to_le32(x) (x)
31+#else
32+# define cpu_to_le16(x) ((((x) >> 8) & 0xffu) | (((x) & 0xffu) << 8))
33+# define cpu_to_le32(x) \
34+	((((x) & 0xff000000u) >> 24) | (((x) & 0x00ff0000u) >>  8) | \
35+	 (((x) & 0x0000ff00u) <<  8) | (((x) & 0x000000ffu) << 24))
36+#endif
37
38 struct usb_handle
39 {
40Index: b/core/adbd/usb_linux_client.c
41===================================================================
42--- a/core/adbd/usb_linux_client.c
43+++ b/core/adbd/usb_linux_client.c
44@@ -34,8 +34,15 @@
45 #define MAX_PACKET_SIZE_FS	64
46 #define MAX_PACKET_SIZE_HS	512
47
48-#define cpu_to_le16(x)  htole16(x)
49-#define cpu_to_le32(x)  htole32(x)
50+#if __BYTE_ORDER == __LITTLE_ENDIAN
51+# define cpu_to_le16(x) (x)
52+# define cpu_to_le32(x) (x)
53+#else
54+# define cpu_to_le16(x) ((((x) >> 8) & 0xffu) | (((x) & 0xffu) << 8))
55+# define cpu_to_le32(x) \
56+	((((x) & 0xff000000u) >> 24) | (((x) & 0x00ff0000u) >>  8) | \
57+	 (((x) & 0x0000ff00u) <<  8) | (((x) & 0x000000ffu) << 24))
58+#endif
59
60 struct usb_handle
61 {
62