Skip to contentSkip to conversation

shared/util: Add byte-swapping macros for constants

PATCH In review Bastien Nocera 4 participants 6 messages Feb 9 – Feb 10 Bluetooth drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 shared/util: Add byte-swapping macros for constants

We can't use functions to populate constants, so add new helpers for the various __bswap_constant_XX macros available for that purpose.

---
 src/shared/util.h | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
src/shared/util.h +24 -0
index c480351d6e9f..8683684831c5 100644
@@ -29,12 +29,24 @@
 #define cpu_to_le16(val) (val)
 #define cpu_to_le32(val) (val)
 #define cpu_to_le64(val) (val)
+#define le16_constant_to_cpu(val) (val)
+#define le32_constant_to_cpu(val) (val)
+#define le64_constant_to_cpu(val) (val)
+#define cpu_constant_to_le16(val) (val)
+#define cpu_constant_to_le32(val) (val)
+#define cpu_constant_to_le64(val) (val)
 #define be16_to_cpu(val) bswap_16(val)
 #define be32_to_cpu(val) bswap_32(val)
 #define be64_to_cpu(val) bswap_64(val)
 #define cpu_to_be16(val) bswap_16(val)
 #define cpu_to_be32(val) bswap_32(val)
 #define cpu_to_be64(val) bswap_64(val)
+#define be16_constant_to_cpu(val) __bswap_constant_16(val)
+#define be32_constant_to_cpu(val) __bswap_constant_32(val)
+#define be64_constant_to_cpu(val) __bswap_constant_64(val)
+#define cpu_constant_to_be16(val) __bswap_constant_16(val)
+#define cpu_constant_to_be32(val) __bswap_constant_32(val)
+#define cpu_constant_to_be64(val) __bswap_constant_64(val)
 #elif __BYTE_ORDER == __BIG_ENDIAN
 #define le16_to_cpu(val) bswap_16(val)
 #define le32_to_cpu(val) bswap_32(val)
@@ -42,12 +54,24 @@
 #define cpu_to_le16(val) bswap_16(val)
 #define cpu_to_le32(val) bswap_32(val)
 #define cpu_to_le64(val) bswap_64(val)
+#define le16_constant_to_cpu(val) __bswap_constant_16(val)
+#define le32_constant_to_cpu(val) __bswap_constant_32(val)
+#define le64_constant_to_cpu(val) __bswap_constant_64(val)
+#define cpu_constant_to_le16(val) __bswap_constant_16(val)
+#define cpu_constant_to_le32(val) __bswap_constant_32(val)
+#define cpu_constant_to_le64(val) __bswap_constant_64(val)
 #define be16_to_cpu(val) (val)
 #define be32_to_cpu(val) (val)
 #define be64_to_cpu(val) (val)
 #define cpu_to_be16(val) (val)
 #define cpu_to_be32(val) (val)
 #define cpu_to_be64(val) (val)
+#define be16_constant_to_cpu(val) (val)
+#define be32_constant_to_cpu(val) (val)
+#define be64_constant_to_cpu(val) (val)
+#define cpu_constant_to_be16(val) (val)
+#define cpu_constant_to_be32(val) (val)
+#define cpu_constant_to_be64(val) (val)
 #else
 #error "Unknown byte order"
 #endif
Signature · 1 line
2.52.0
#1 lore raw

PATCH 2/2 emulator: Fix compilation on big endian systems

Use new helper macros for byteswapping constants.

This fixes the build on big endian systems like s390x:

emulator/bthost.c: In function ‘bthost_setup_sco’:
./src/shared/util.h:43:26: error: initializer element is not constant
43 | #define cpu_to_le32(val) bswap_32(val)

      |                          ^~~~~~~~

etc.

Fixes 85888a8357ea ("bthost: add bthost_setup_sco() and accept

incoming eSCO")

---
 emulator/bthost.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
emulator/bthost.c +8 -8
index d09ad1e76c50..37865f61bc52 100644
@@ -3562,18 +3562,18 @@ int bthost_setup_sco(struct bthost *bthost, uint16_t acl_handle,
 {
 	static const struct bt_hci_cmd_setup_sync_conn settings[] = {
 		{
-			.tx_bandwidth = cpu_to_le32(0x00001f40),
-			.rx_bandwidth = cpu_to_le32(0x00001f40),
-			.max_latency = cpu_to_le16(0x000a),
+			.tx_bandwidth = cpu_constant_to_le32(0x00001f40),
+			.rx_bandwidth = cpu_constant_to_le32(0x00001f40),
+			.max_latency = cpu_constant_to_le16(0x000a),
 			.retrans_effort = 0x01,
-			.voice_setting = cpu_to_le16(BT_VOICE_CVSD_16BIT),
+			.voice_setting = cpu_constant_to_le16(BT_VOICE_CVSD_16BIT),
 		},
 		{
-			.tx_bandwidth = cpu_to_le32(0x00001f40),
-			.rx_bandwidth = cpu_to_le32(0x00001f40),
-			.max_latency = cpu_to_le16(0x000d),
+			.tx_bandwidth = cpu_constant_to_le32(0x00001f40),
+			.rx_bandwidth = cpu_constant_to_le32(0x00001f40),
+			.max_latency = cpu_constant_to_le16(0x000d),
 			.retrans_effort = 0x02,
-			.voice_setting = cpu_to_le16(BT_VOICE_TRANSPARENT),
+			.voice_setting = cpu_constant_to_le16(BT_VOICE_TRANSPARENT),
 		}
 	};
 	struct bt_hci_cmd_setup_sync_conn cmd;
Signature · 1 line
2.52.0
#2 lore raw

Re: [PATCH BlueZ 2/2] emulator: Fix compilation on big endian systems

Hi Bastien,

Quoted 36 lines · Bastien Nocera

Use new helper macros for byteswapping constants.

This fixes the build on big endian systems like s390x:

emulator/bthost.c: In function ‘bthost_setup_sco’:
./src/shared/util.h:43:26: error: initializer element is not constant
43 | #define cpu_to_le32(val) bswap_32(val)

      |                          ^~~~~~~~

etc.

Fixes 85888a8357ea ("bthost: add bthost_setup_sco() and accept

incoming eSCO")

---
 emulator/bthost.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
emulator/bthost.c +4 -4
index d09ad1e76c50..37865f61bc52 100644
@@ -3562,18 +3562,18 @@ int bthost_setup_sco(struct bthost *bthost, uint16_t acl_handle,
 {
        static const struct bt_hci_cmd_setup_sync_conn settings[] = {
                {
-                       .tx_bandwidth = cpu_to_le32(0x00001f40),
-                       .rx_bandwidth = cpu_to_le32(0x00001f40),
-                       .max_latency = cpu_to_le16(0x000a),
+                       .tx_bandwidth = cpu_constant_to_le32(0x00001f40),
+                       .rx_bandwidth = cpu_constant_to_le32(0x00001f40),
+                       .max_latency = cpu_constant_to_le16(0x000a),
                        .retrans_effort = 0x01,
-                       .voice_setting = cpu_to_le16(BT_VOICE_CVSD_16BIT),
+                       .voice_setting = cpu_constant_to_le16(BT_VOICE_CVSD_16BIT),
                },
                {

emulator/bthost.c int bthost_setup_sco(struct bthost *bthost, uint16_t acl_handle, · line 3577

Quoted 10 lines
emulator/bthost.c +4 -4
-                       .tx_bandwidth = cpu_to_le32(0x00001f40),
-                       .rx_bandwidth = cpu_to_le32(0x00001f40),
-                       .max_latency = cpu_to_le16(0x000d),
+                       .tx_bandwidth = cpu_constant_to_le32(0x00001f40),
+                       .rx_bandwidth = cpu_constant_to_le32(0x00001f40),
+                       .max_latency = cpu_constant_to_le16(0x000d),
                        .retrans_effort = 0x02,
-                       .voice_setting = cpu_to_le16(BT_VOICE_TRANSPARENT),
+                       .voice_setting = cpu_constant_to_le16(BT_VOICE_TRANSPARENT),
                }

Perhaps we should change the above, I don't think it is really useful to have a table like this can then do memcpy, instead we can probably just use a switch (setting) and then initialize the values in the cmd directly, that way we can use the regular method of converting since it would not be required to be a constant.

Quoted 6 lines
emulator/bthost.c +0 -1
        };
        struct bt_hci_cmd_setup_sync_conn cmd;
--

2.52.0

Signature · 1 line
Luiz Augusto von Dentz
#3 lore raw

Re: [PATCH BlueZ 2/2] emulator: Fix compilation on big endian systems

In reply to Bastien Nocera

Hi,

ma, 2026-02-09 kello 21:33 +0100, Bastien Nocera kirjoitti:

Quoted 23 lines

Use new helper macros for byteswapping constants.

This fixes the build on big endian systems like s390x:

emulator/bthost.c: In function ‘bthost_setup_sco’:
./src/shared/util.h:43:26: error: initializer element is not constant 43 | #define cpu_to_le32(val) bswap_32(val)
      |                          ^~~~~~~~
etc.

Fixes 85888a8357ea ("bthost: add bthost_setup_sco() and accept

incoming eSCO")
---
 emulator/bthost.c | 16 ++++++++--------

 1 file changed, 8 insertions(+), 8 deletions(-)
emulator/bthost.c +0 -0
index d09ad1e76c50..37865f61bc52 100644
@@ -3562,18 +3562,18 @@ int bthost_setup_sco(struct bthost *bthost,
 uint16_t acl_handle,
  {
Quoted 2 lines

  static const struct bt_hci_cmd_setup_sync_conn settings[] = {

Removing the "static" here would be another way to fix it, as it doesn't really need to be static here.

Quoted 32 lines

  {

emulator/bthost.c +1 -3
-			.tx_bandwidth = cpu_to_le32(0x00001f40),
-			.rx_bandwidth = cpu_to_le32(0x00001f40),
-			.max_latency = cpu_to_le16(0x000a),
+			.tx_bandwidth =

cpu_constant_to_le32(0x00001f40),

emulator/bthost.c +1 -0
+			.rx_bandwidth =

cpu_constant_to_le32(0x00001f40),

emulator/bthost.c +1 -0
+			.max_latency = cpu_constant_to_le16(0x000a),

  .retrans_effort = 0x01,

emulator/bthost.c +0 -1
-			.voice_setting =

cpu_to_le16(BT_VOICE_CVSD_16BIT),

emulator/bthost.c +1 -0
+			.voice_setting =

cpu_constant_to_le16(BT_VOICE_CVSD_16BIT),
  },
  {

emulator/bthost.c +1 -3
-			.tx_bandwidth = cpu_to_le32(0x00001f40),
-			.rx_bandwidth = cpu_to_le32(0x00001f40),
-			.max_latency = cpu_to_le16(0x000d),
+			.tx_bandwidth =

cpu_constant_to_le32(0x00001f40),

emulator/bthost.c +1 -0
+			.rx_bandwidth =

cpu_constant_to_le32(0x00001f40),

emulator/bthost.c +1 -0
+			.max_latency = cpu_constant_to_le16(0x000d),

  .retrans_effort = 0x02,

emulator/bthost.c +0 -1
-			.voice_setting =

cpu_to_le16(BT_VOICE_TRANSPARENT),

emulator/bthost.c +1 -0
+			.voice_setting =

cpu_constant_to_le16(BT_VOICE_TRANSPARENT),
  }
  };
  struct bt_hci_cmd_setup_sync_conn cmd;

Signature · 1 line
Pauli Virtanen
#4 lore raw

Re: [PATCH BlueZ 2/2] emulator: Fix compilation on big endian systems

Quoted 20 lines · Pauli Virtanen

Hi,

ma, 2026-02-09 kello 21:33 +0100, Bastien Nocera kirjoitti:

Quoted 17 lines

Use new helper macros for byteswapping constants.

This fixes the build on big endian systems like s390x:

emulator/bthost.c: In function ‘bthost_setup_sco’:
./src/shared/util.h:43:26: error: initializer element is not constant
   43 | #define cpu_to_le32(val) bswap_32(val)
      |                          ^~~~~~~~
etc.

Fixes 85888a8357ea ("bthost: add bthost_setup_sco() and accept

incoming eSCO")
---
 emulator/bthost.c | 16 ++++++++--------

 1 file changed, 8 insertions(+), 8 deletions(-)
Quoted 13 lines
Quoted 10 lines
emulator/bthost.c +0 -0
index d09ad1e76c50..37865f61bc52 100644
@@ -3562,18 +3562,18 @@ int bthost_setup_sco(struct bthost *bthost,
 uint16_t acl_handle,
  {

  static const struct bt_hci_cmd_setup_sync_conn settings[] =
{

Removing the "static" here would be another way to fix it, as it doesn't really need to be static here.

That looks like it's enough indeed, let me respin a patch.

#5 lore raw
In reply to Bastien Nocera

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=1052425

---Test result---

Test Summary:

CheckPatch                    PENDING   0.45 seconds
GitLint                       PENDING   0.51 seconds
BuildEll                      PASS      20.62 seconds
BluezMake                     PASS      676.16 seconds
MakeCheck                     PASS      18.46 seconds
MakeDistcheck                 PASS      250.39 seconds
CheckValgrind                 PASS      299.39 seconds
CheckSmatch                   WARNING   357.87 seconds
bluezmakeextell               PASS      186.11 seconds
IncrementalBuild              PENDING   0.46 seconds
ScanBuild                     PASS      1046.68 seconds

Details
##############################
Test: CheckPatch - PENDING
Desc: Run checkpatch.pl script Output:

##############################
Test: GitLint - PENDING
Desc: Run gitlint
Output:

##############################
Test: CheckSmatch - WARNING
Desc: Run smatch tool with source
Output:
emulator/bthost.c:700:28: warning: Variable length array is used.emulator/bthost.c:701:32: warning: Variable length array is used.emulator/bthost.c:918:28: warning: Variable length array is used.emulator/bthost.c:952:28: warning: Variable length array is used.emulator/bthost.c:953:32: warning: Variable length array is used.
##############################
Test: IncrementalBuild - PENDING
Desc: Incremental build with the patches in the series
Output:

---
Regards,
Linux Bluetooth