Skip to contentSkip to conversation

iio: hid-sensor: standardize scan_type initialization

PATCH In review Natália Salvino André 3 participants 14 messages May 19 – May 22

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v4 cover iio: hid-sensor: standardize scan_type initialization

This series refactors the HID sensor drivers to standardize the initialization of the iio_chan_spec scan_type structure using compound literals.

This change improves code readability and ensures that all fields of the scan_type structure are properly zero-initialized, allowing the removal of local helper functions.

Additionally, the channel initialization loops for hid-sensor-accel-3d and hid-sensor-gyro-3d were cleaned up to iterate directly over the scan indices, eliminating redundant index-offset logic.

---

Changes in v4:

- Fixed a bug in the sensor_hub_input_get_attribute_info() call where passing the raw loop index 'ch' broke the HID report field lookup in hid-sensor-accel-3d. Restored the proper HID usage ID offset calculation using HID_USAGE_SENSOR_ACCEL_X_AXIS + ch
- Cleaned up the channel initialization loop to iterate directly over the scan indices in hid-sensor-gyro-3d for consistency

Changes in v3:

- Dropped the global helper function implementation from
hid-sensor-attributes.c following maintainer feedback.
- Shifted to local refactoring inside the drivers using compound
literals and the macros BYTES_TO_BITS() and BITS_PER_TYPE()
- Refactored the channel initialization loop in hid-sensor-accel-3d to
iterate directly over the scan enums instead of using 0-based
index offsets
- v3 link: https://lore.kernel.org/linux-iio/[email protected]/T/#t

Changes in v2:

- Modified the helper function to use the BITS_PER_BYTE macro instead of magic numbers for bit calculations
- Updated the scan_type structure field assignment from '.sign' to '.format' based on recent subsystem renaming feedback

Natália Salvino André (6):
iio: accel: HID: hid-sensor-accel-3d: Refactor channel initialization
iio: gyro: HID: hid-sensor-gyro-3d: Refactor channel initialization
iio: light: HID: hid-sensor-als: Refactor channel initialization
iio: light: HID: hid-sensor-prox: Refactor channel initialization
iio: magnetometer: HID: hid-sensor-magn-3d: Refactor channel
initialization
iio: pressure: HID: hid-sensor-press: Refactor channel initialization

 drivers/iio/accel/hid-sensor-accel-3d.c       | 27 +++++++------------
 drivers/iio/gyro/hid-sensor-gyro-3d.c         | 27 +++++++------------
 drivers/iio/light/hid-sensor-als.c            | 18 +++++--------
 drivers/iio/light/hid-sensor-prox.c           | 19 +++++--------
 drivers/iio/magnetometer/hid-sensor-magn-3d.c | 20 +++++---------
 drivers/iio/pressure/hid-sensor-press.c       | 19 +++++--------
 6 files changed, 42 insertions(+), 88 deletions(-)
Signature · 1 line
2.51.0
#1 lore raw

PATCH v4 1/6 iio: accel: HID: hid-sensor-accel-3d: Refactor channel initialization

Clean up the channel initialization loop and replace the local accel_3d_adjust_channel_bit_mask() function with a compound literal to improve code readability.

---
 drivers/iio/accel/hid-sensor-accel-3d.c | 27 +++++++++----------------
 1 file changed, 9 insertions(+), 18 deletions(-)
drivers/iio/accel/hid-sensor-accel-3d.c +9 -18
index 2ff591b3458f..2bf05ab5235e 100644
@@ -3,6 +3,7 @@
  * HID Sensors Driver
  * Copyright (c) 2012, Intel Corporation.
  */
+#include <linux/bitops.h>
 #include <linux/device.h>
 #include <linux/platform_device.h>
 #include <linux/module.h>
@@ -119,17 +120,6 @@ static const struct iio_chan_spec gravity_channels[] = {
 	IIO_CHAN_SOFT_TIMESTAMP(CHANNEL_SCAN_INDEX_TIMESTAMP),
 };
 
-/* Adjust channel real bits based on report descriptor */
-static void accel_3d_adjust_channel_bit_mask(struct iio_chan_spec *channels,
-						int channel, int size)
-{
-	channels[channel].scan_type.sign = 's';
-	/* Real storage bits will change based on the report desc. */
-	channels[channel].scan_type.realbits = size * 8;
-	/* Maximum size of a sample to capture is u32 */
-	channels[channel].scan_type.storagebits = sizeof(u32) * 8;
-}
-
 /* Channel read_raw handler */
 static int accel_3d_read_raw(struct iio_dev *indio_dev,
 			      struct iio_chan_spec const *chan,
@@ -297,19 +287,20 @@ static int accel_3d_parse_report(struct platform_device *pdev,
 				struct accel_3d_state *st)
 {
 	int ret;
-	int i;
 
-	for (i = 0; i <= CHANNEL_SCAN_INDEX_Z; ++i) {
+	for (unsigned int ch = CHANNEL_SCAN_INDEX_X; ch <= CHANNEL_SCAN_INDEX_Z; ch++) {
 		ret = sensor_hub_input_get_attribute_info(hsdev,
 				HID_INPUT_REPORT,
 				usage_id,
-				HID_USAGE_SENSOR_ACCEL_X_AXIS + i,
-				&st->accel[CHANNEL_SCAN_INDEX_X + i]);
+				HID_USAGE_SENSOR_ACCEL_X_AXIS + ch,
+				&st->accel[ch]);
 		if (ret < 0)
 			break;
-		accel_3d_adjust_channel_bit_mask(channels,
-				CHANNEL_SCAN_INDEX_X + i,
-				st->accel[CHANNEL_SCAN_INDEX_X + i].size);
+		channels[ch].scan_type = (struct iio_scan_type) {
+			.format = 's',
+			.realbits = BYTES_TO_BITS(st->accel[ch].size),
+			.storagebits = BITS_PER_TYPE(u32),
+		};
 	}
 	dev_dbg(&pdev->dev, "accel_3d %x:%x, %x:%x, %x:%x\n",
 			st->accel[0].index,
Signature · 1 line
2.51.0
#2 lore raw

Re: [PATCH v4 1/6] iio: accel: HID: hid-sensor-accel-3d: Refactor channel initialization

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The patch introduces a compilation error by using a non-existent field `.format` instead of `.sign`…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The patch introduces a compilation error by using a non-existent field .format instead of .sign in the initialization of struct iio_scan_type. --

commit 5d0645bc673099b585d567599624c53910242efc
Author: Natália Salvino André <[email protected]>

iio: accel: HID: hid-sensor-accel-3d: Refactor channel initialization

Clean up the channel initialization loop and replace the local accel_3d_adjust_channel_bit_mask() function with a compound literal to improve code readability.

Quoted 4 lines
drivers/iio/accel/hid-sensor-accel-3d.c +0 -0
index 2ff591b3458f7..2bf05ab5235ef 100644

[ ... ]

Quoted 12 lines
drivers/iio/accel/hid-sensor-accel-3d.c +1 -3
@@ -297,19 +287,20 @@ static int accel_3d_parse_report(struct platform_device *pdev,
 				struct accel_3d_state *st)
 {
 	int ret;
-	int i;
 
-	for (i = 0; i <= CHANNEL_SCAN_INDEX_Z; ++i) {
+	for (unsigned int ch = CHANNEL_SCAN_INDEX_X; ch <= CHANNEL_SCAN_INDEX_Z; ch++) {
 		ret = sensor_hub_input_get_attribute_info(hsdev,
 				HID_INPUT_REPORT,
 				usage_id,
-				HID_USAGE_SENSOR_ACCEL_X_AXIS + i,

drivers/iio/accel/hid-sensor-accel-3d.c static int accel_3d_parse_report(struct platform_device *pdev, · line 300

Quoted 10 lines
drivers/iio/accel/hid-sensor-accel-3d.c +4 -4
-				&st->accel[CHANNEL_SCAN_INDEX_X + i]);
+				HID_USAGE_SENSOR_ACCEL_X_AXIS + ch,
+				&st->accel[ch]);
 		if (ret < 0)
 			break;
-		accel_3d_adjust_channel_bit_mask(channels,
-				CHANNEL_SCAN_INDEX_X + i,
-				st->accel[CHANNEL_SCAN_INDEX_X + i].size);
+		channels[ch].scan_type = (struct iio_scan_type) {
+			.format = 's',

[Severity: Low]
Does this code result in a compilation error? Looking at struct iio_scan_type defined in include/linux/iio/iio.h, it contains a sign member but no format member. Should this be .sign = 's' instead?

Quoted 4 lines
drivers/iio/accel/hid-sensor-accel-3d.c +3 -0
+			.realbits = BYTES_TO_BITS(st->accel[ch].size),
+			.storagebits = BITS_PER_TYPE(u32),
+		};
 	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

PATCH v4 2/6 iio: gyro: HID: hid-sensor-gyro-3d: Refactor channel initialization

In reply to Natália Salvino André

Replace the local gyro_3d_adjust_channel_bit_mask() function with a compound literal for scan_type initialization to improve code readability. Additionaly, clean up the channel initialization loop by iterating directly over the channel scan indices.

---
 drivers/iio/gyro/hid-sensor-gyro-3d.c | 27 +++++++++------------------
 1 file changed, 9 insertions(+), 18 deletions(-)
drivers/iio/gyro/hid-sensor-gyro-3d.c +9 -18
index c340cc899a7c..e48c25c87b6d 100644
@@ -3,6 +3,7 @@
  * HID Sensors Driver
  * Copyright (c) 2012, Intel Corporation.
  */
+#include <linux/bitops.h>
 #include <linux/device.h>
 #include <linux/platform_device.h>
 #include <linux/module.h>
@@ -82,17 +83,6 @@ static const struct iio_chan_spec gyro_3d_channels[] = {
 	IIO_CHAN_SOFT_TIMESTAMP(CHANNEL_SCAN_INDEX_TIMESTAMP)
 };
 
-/* Adjust channel real bits based on report descriptor */
-static void gyro_3d_adjust_channel_bit_mask(struct iio_chan_spec *channels,
-						int channel, int size)
-{
-	channels[channel].scan_type.sign = 's';
-	/* Real storage bits will change based on the report desc. */
-	channels[channel].scan_type.realbits = size * 8;
-	/* Maximum size of a sample to capture is u32 */
-	channels[channel].scan_type.storagebits = sizeof(u32) * 8;
-}
-
 /* Channel read_raw handler */
 static int gyro_3d_read_raw(struct iio_dev *indio_dev,
 			      struct iio_chan_spec const *chan,
@@ -248,19 +238,20 @@ static int gyro_3d_parse_report(struct platform_device *pdev,
 				struct gyro_3d_state *st)
 {
 	int ret;
-	int i;
 
-	for (i = 0; i <= CHANNEL_SCAN_INDEX_Z; ++i) {
+	for (unsigned int ch = CHANNEL_SCAN_INDEX_X; ch <= CHANNEL_SCAN_INDEX_Z; ch++) {
 		ret = sensor_hub_input_get_attribute_info(hsdev,
 				HID_INPUT_REPORT,
 				usage_id,
-				HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS + i,
-				&st->gyro[CHANNEL_SCAN_INDEX_X + i]);
+				HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS + ch,
+				&st->gyro[ch]);
 		if (ret < 0)
 			break;
-		gyro_3d_adjust_channel_bit_mask(channels,
-				CHANNEL_SCAN_INDEX_X + i,
-				st->gyro[CHANNEL_SCAN_INDEX_X + i].size);
+		channels[ch].scan_type = (struct iio_scan_type) {
+			.format = 's',
+			.realbits = BYTES_TO_BITS(st->gyro[ch].size),
+			.storagebits = BITS_PER_TYPE(u32),
+		};
 	}
 	dev_dbg(&pdev->dev, "gyro_3d %x:%x, %x:%x, %x:%x\n",
 			st->gyro[0].index,
Signature · 1 line
2.51.0
#4 lore raw

Re: [PATCH v4 2/6] iio: gyro: HID: hid-sensor-gyro-3d: Refactor channel initialization

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

New issues:
- [Low] The struct iio_scan_type is incorrectly initialized with a non-existent .format member, causing a build failure.

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but a teardown race condition exists when the driver is unbound, leading to a potential Use-After-Free of gyro_state inside the HID raw event handler. --

commit fa4886d3bbba316658546fb6162186593b204b9c
Author: Natália Salvino André <[email protected]>

iio: gyro: HID: hid-sensor-gyro-3d: Refactor channel initialization

This commit replaces the local gyro_3d_adjust_channel_bit_mask() function with a compound literal for scan_type initialization to improve code readability. It also cleans up the channel initialization loop by iterating directly over the channel scan indices.

Quoted 4 lines
drivers/iio/gyro/hid-sensor-gyro-3d.c +0 -0
index c43990c518f77..ee7bde8620b41 100644

[ ... ]

Quoted 12 lines
drivers/iio/gyro/hid-sensor-gyro-3d.c +1 -3
@@ -248,19 +238,20 @@ static int gyro_3d_parse_report(struct platform_device *pdev,
 				struct gyro_3d_state *st)
 {
 	int ret;
-	int i;
 
-	for (i = 0; i <= CHANNEL_SCAN_INDEX_Z; ++i) {
+	for (unsigned int ch = CHANNEL_SCAN_INDEX_X; ch <= CHANNEL_SCAN_INDEX_Z; ch++) {
 		ret = sensor_hub_input_get_attribute_info(hsdev,
 				HID_INPUT_REPORT,
 				usage_id,
-				HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS + i,

drivers/iio/gyro/hid-sensor-gyro-3d.c static int gyro_3d_parse_report(struct platform_device *pdev, · line 251

Quoted 10 lines
drivers/iio/gyro/hid-sensor-gyro-3d.c +4 -4
-				&st->gyro[CHANNEL_SCAN_INDEX_X + i]);
+				HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS + ch,
+				&st->gyro[ch]);
 		if (ret < 0)
 			break;
-		gyro_3d_adjust_channel_bit_mask(channels,
-				CHANNEL_SCAN_INDEX_X + i,
-				st->gyro[CHANNEL_SCAN_INDEX_X + i].size);
+		channels[ch].scan_type = (struct iio_scan_type) {
+			.format = 's',

[Severity: Low]
Does this initialization cause a build failure?

Looking at struct iio_scan_type, it designates the field for signedness as sign, not format. The original code correctly assigned 's' to channels[channel].scan_type.sign. Using format here will likely cause a compiler error about having no member named format.

drivers/iio/gyro/hid-sensor-gyro-3d.c static int gyro_3d_parse_report(struct platform_device *pdev, · line 255

Quoted 4 lines
drivers/iio/gyro/hid-sensor-gyro-3d.c +3 -0
+			.realbits = BYTES_TO_BITS(st->gyro[ch].size),
+			.storagebits = BITS_PER_TYPE(u32),
+		};
 	}

[Severity: High]
This isn't a bug introduced by this patch, but could a teardown race condition exist when the driver is unbound, leading to a potential use-after-free of gyro_state inside the HID raw event handler?

When the driver is manually unbound, hid_gyro_3d_remove() calls sensor_hub_remove_callback(). This core function unregisters the callback list entry but does not flush or synchronize with concurrently running sensor_hub_raw_event() asynchronous dispatches.

Consequently, remove completes and devres proceeds to free indio_dev, which embeds gyro_state and the callback structure. A concurrent sensor_hub_raw_event() that fetched the callback pointer prior to removal (after dropping dyn_callback_lock) could then execute callback->capture_sample() and callback->send_event() on freed memory.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#5 lore raw

PATCH v4 3/6 iio: light: HID: hid-sensor-als: Refactor channel initialization

In reply to Natália Salvino André

Replace the local als_adjust_channel_bit_mask() function with a compound literal for scan_type initialization to improve code readability.

---
 drivers/iio/light/hid-sensor-als.c | 18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)
drivers/iio/light/hid-sensor-als.c +6 -12
index 384572844162..d72e260b8266 100644
@@ -3,6 +3,7 @@
  * HID Sensors Driver
  * Copyright (c) 2012, Intel Corporation.
  */
+#include <linux/bitops.h>
 #include <linux/device.h>
 #include <linux/platform_device.h>
 #include <linux/module.h>
@@ -117,17 +118,6 @@ static const struct iio_chan_spec als_channels[] = {
 	IIO_CHAN_SOFT_TIMESTAMP(CHANNEL_SCAN_INDEX_TIMESTAMP)
 };
 
-/* Adjust channel real bits based on report descriptor */
-static void als_adjust_channel_bit_mask(struct iio_chan_spec *channels,
-					int channel, int size)
-{
-	channels[channel].scan_type.sign = 's';
-	/* Real storage bits will change based on the report desc. */
-	channels[channel].scan_type.realbits = size * 8;
-	/* Maximum size of a sample to capture is u32 */
-	channels[channel].scan_type.storagebits = sizeof(u32) * 8;
-}
-
 /* Channel read_raw handler */
 static int als_read_raw(struct iio_dev *indio_dev,
 			      struct iio_chan_spec const *chan,
@@ -335,7 +325,11 @@ static int als_parse_report(struct platform_device *pdev,
 
 		channels[index] = als_channels[i];
 		st->als_scan_mask[0] |= BIT(i);
-		als_adjust_channel_bit_mask(channels, index, st->als[i].size);
+		channels[index].scan_type = (struct iio_scan_type) {
+			.format = 's',
+			.realbits = BYTES_TO_BITS(st->als[i].size),
+			.storagebits = BITS_PER_TYPE(u32),
+		};
 		++index;
 
 		dev_dbg(&pdev->dev, "als %x:%x\n", st->als[i].index,
Signature · 1 line
2.51.0
#6 lore raw

Re: [PATCH v4 3/6] iio: light: HID: hid-sensor-als: Refactor channel initialization

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Incorrect member name `.format` used in `struct iio_scan_type` initialization. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Incorrect member name .format used in struct iio_scan_type initialization. --

commit 6e85d51443d3e127d310915780d0c6f113e03de0
Author: Natália Salvino André <[email protected]>

iio: light: HID: hid-sensor-als: Refactor channel initialization

This patch refactors the channel initialization by replacing the local function als_adjust_channel_bit_mask() with a compound literal to initialize scan_type.

Quoted 4 lines
drivers/iio/light/hid-sensor-als.c +0 -0
index 830e5ae7f34a7..e5b4c5e80f1cf 100644

[ ... ]

drivers/iio/light/hid-sensor-als.c static int als_parse_report(struct platform_device *pdev, · line 328

Quoted 7 lines
drivers/iio/light/hid-sensor-als.c +2 -1
@@ -334,7 +324,11 @@ static int als_parse_report(struct platform_device *pdev,
 
 		channels[index] = als_channels[i];
 		st->als_scan_mask[0] |= BIT(i);
-		als_adjust_channel_bit_mask(channels, index, st->als[i].size);
+		channels[index].scan_type = (struct iio_scan_type) {
+			.format = 's',

[Severity: Low]
Does struct iio_scan_type have a .format member? Looking at include/linux/iio/iio.h, the member for indicating signedness is .sign, which was used in the original als_adjust_channel_bit_mask() code.

Will this cause a compilation failure due to an unknown field?

Quoted 4 lines
drivers/iio/light/hid-sensor-als.c +3 -0
+			.realbits = BYTES_TO_BITS(st->als[i].size),
+			.storagebits = BITS_PER_TYPE(u32),
+		};
 		++index;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#7 lore raw

PATCH v4 4/6 iio: light: HID: hid-sensor-prox: Refactor channel initialization

In reply to Natália Salvino André

Replace the local prox_adjust_channel_bit_mask() function with a compound literal for scan_type initialization to improve code readability.

---
 drivers/iio/light/hid-sensor-prox.c | 19 ++++++-------------
 1 file changed, 6 insertions(+), 13 deletions(-)
drivers/iio/light/hid-sensor-prox.c +6 -13
index efa904a70d0e..edc9274a2c07 100644
@@ -3,6 +3,7 @@
  * HID Sensors Driver
  * Copyright (c) 2014, Intel Corporation.
  */
+#include <linux/bitops.h>
 #include <linux/device.h>
 #include <linux/platform_device.h>
 #include <linux/module.h>
@@ -67,17 +68,6 @@ static const struct iio_chan_spec prox_channels[] = {
 	PROX_CHANNEL(false, 0),
 };
 
-/* Adjust channel real bits based on report descriptor */
-static void prox_adjust_channel_bit_mask(struct iio_chan_spec *channels,
-					int channel, int size)
-{
-	channels[channel].scan_type.sign = 's';
-	/* Real storage bits will change based on the report desc. */
-	channels[channel].scan_type.realbits = size * 8;
-	/* Maximum size of a sample to capture is u32 */
-	channels[channel].scan_type.storagebits = sizeof(u32) * 8;
-}
-
 /* Channel read_raw handler */
 static int prox_read_raw(struct iio_dev *indio_dev,
 			      struct iio_chan_spec const *chan,
@@ -250,8 +240,11 @@ static int prox_parse_report(struct platform_device *pdev,
 		st->scan_mask[0] |= BIT(index);
 		channels[index] = prox_channels[i];
 		channels[index].scan_index = index;
-		prox_adjust_channel_bit_mask(channels, index,
-					     st->prox_attr[index].size);
+		channels[index].scan_type = (struct iio_scan_type) {
+			.format = 's',
+			.realbits = BYTES_TO_BITS(st->prox_attr[index].size),
+			.storagebits = BITS_PER_TYPE(u32),
+		};
 		dev_dbg(&pdev->dev, "prox %x:%x\n", st->prox_attr[index].index,
 			st->prox_attr[index].report_id);
 		st->scale_precision[index] =
Signature · 1 line
2.51.0
#8 lore raw

Re: [PATCH v4 4/6] iio: light: HID: hid-sensor-prox: Refactor channel initialization

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The patch introduces a compiler error by using an invalid member name `.format` in a compound…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The patch introduces a compiler error by using an invalid member name .format in a compound literal for struct iio_scan_type. --

commit 79e0a9b7358ae7d1e9e80ed657afdcf93d6d8469
Author: Natália Salvino André <[email protected]>

iio: light: HID: hid-sensor-prox: Refactor channel initialization

This commit replaces the local prox_adjust_channel_bit_mask() function with a compound literal for scan_type initialization. It aims to improve code readability by directly assigning the struct fields.

Quoted 4 lines
drivers/iio/light/hid-sensor-prox.c +0 -0
index efa904a70d0ef..edc9274a2c076 100644

[ ... ]

drivers/iio/light/hid-sensor-prox.c static int prox_parse_report(struct platform_device *pdev, · line 244

Quoted 8 lines
drivers/iio/light/hid-sensor-prox.c +2 -2
@@ -250,8 +240,11 @@ static int prox_parse_report(struct platform_device *pdev,
 		st->scan_mask[0] |= BIT(index);
 		channels[index] = prox_channels[i];
 		channels[index].scan_index = index;
-		prox_adjust_channel_bit_mask(channels, index,
-					     st->prox_attr[index].size);
+		channels[index].scan_type = (struct iio_scan_type) {
+			.format = 's',

[Severity: Low]
Will this cause a compiler error? struct iio_scan_type does not appear to have a member named format. The previous code set the sign field to 's', so perhaps this should be .sign instead of .format?

Quoted 6 lines
drivers/iio/light/hid-sensor-prox.c +3 -0
+			.realbits = BYTES_TO_BITS(st->prox_attr[index].size),
+			.storagebits = BITS_PER_TYPE(u32),
+		};
 		dev_dbg(&pdev->dev, "prox %x:%x\n", st->prox_attr[index].index,
 			st->prox_attr[index].report_id);
 		st->scale_precision[index] =
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#9 lore raw

PATCH v4 5/6 iio: magnetometer: HID: hid-sensor-magn-3d: Refactor channel initialization

In reply to Natália Salvino André

Replace the local magn_3d_adjust_channel_bit_mask() function with a compound literal for scan_type initialization to improve code readability.

---
 drivers/iio/magnetometer/hid-sensor-magn-3d.c | 20 ++++++-------------
 1 file changed, 6 insertions(+), 14 deletions(-)
drivers/iio/magnetometer/hid-sensor-magn-3d.c +6 -14
index c673f9323e47..3b26dc41dd4c 100644
@@ -3,6 +3,7 @@
  * HID Sensors Driver
  * Copyright (c) 2012, Intel Corporation.
  */
+#include <linux/bitops.h>
 #include <linux/device.h>
 #include <linux/platform_device.h>
 #include <linux/module.h>
@@ -132,17 +133,6 @@ static const struct iio_chan_spec magn_3d_channels[] = {
 	IIO_CHAN_SOFT_TIMESTAMP(7)
 };
 
-/* Adjust channel real bits based on report descriptor */
-static void magn_3d_adjust_channel_bit_mask(struct iio_chan_spec *channels,
-						int channel, int size)
-{
-	channels[channel].scan_type.sign = 's';
-	/* Real storage bits will change based on the report desc. */
-	channels[channel].scan_type.realbits = size * 8;
-	/* Maximum size of a sample to capture is u32 */
-	channels[channel].scan_type.storagebits = sizeof(u32) * 8;
-}
-
 /* Channel read_raw handler */
 static int magn_3d_read_raw(struct iio_dev *indio_dev,
 			      struct iio_chan_spec const *chan,
@@ -418,9 +408,11 @@ static int magn_3d_parse_report(struct platform_device *pdev,
 			if (i != CHANNEL_SCAN_INDEX_TIMESTAMP) {
 				/* Set magn_val_addr to iio value address */
 				st->magn_val_addr[i] = &st->iio_vals[*chan_count];
-				magn_3d_adjust_channel_bit_mask(_channels,
-								*chan_count,
-								st->magn[i].size);
+				_channels[*chan_count].scan_type = (struct iio_scan_type) {
+					.format = 's',
+					.realbits = BYTES_TO_BITS(st->magn[i].size),
+					.storagebits = BITS_PER_TYPE(u32),
+				};
 			}
 			(*chan_count)++;
 		}
Signature · 1 line
2.51.0
#10 lore raw

Re: [PATCH v4 5/6] iio: magnetometer: HID: hid-sensor-magn-3d: Refactor channel initialization

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Invalid struct member name '.format' used in compound literal for struct iio_scan_type. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Invalid struct member name '.format' used in compound literal for struct iio_scan_type. --

commit a348ae7b1670bf238471e69df7f224b48282264e
Author: Natália Salvino André <[email protected]>

iio: magnetometer: HID: hid-sensor-magn-3d: Refactor channel initialization

This commit replaces the local magn_3d_adjust_channel_bit_mask() function with a compound literal for scan_type initialization to improve code readability.

Quoted 4 lines
drivers/iio/magnetometer/hid-sensor-magn-3d.c +0 -0
index c673f9323e476..3b26dc41dd4cf 100644

[ ... ]

drivers/iio/magnetometer/hid-sensor-magn-3d.c static int magn_3d_parse_report(struct platform_device *pdev, · line 412

Quoted 9 lines
drivers/iio/magnetometer/hid-sensor-magn-3d.c +2 -3
@@ -418,9 +408,11 @@ static int magn_3d_parse_report(struct platform_device *pdev,
 			if (i != CHANNEL_SCAN_INDEX_TIMESTAMP) {
 				/* Set magn_val_addr to iio value address */
 				st->magn_val_addr[i] = &st->iio_vals[*chan_count];
-				magn_3d_adjust_channel_bit_mask(_channels,
-								*chan_count,
-								st->magn[i].size);
+				_channels[*chan_count].scan_type = (struct iio_scan_type) {
+					.format = 's',

[Severity: Low]
Will this cause a build failure? Looking at struct iio_scan_type, there is no .format member. The correct field for the sign indicator appears to be .sign, which was used in the original magn_3d_adjust_channel_bit_mask() function.

Quoted 6 lines
drivers/iio/magnetometer/hid-sensor-magn-3d.c +3 -0
+					.realbits = BYTES_TO_BITS(st->magn[i].size),
+					.storagebits = BITS_PER_TYPE(u32),
+				};
 			}
 			(*chan_count)++;
 		}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#11 lore raw

PATCH v4 6/6 iio: pressure: HID: hid-sensor-press: Refactor channel initialization

In reply to Natália Salvino André

Replace the local press_adjust_channel_bit_mask() function with a compound literal for scan_type initialization to improve code readability.

---
 drivers/iio/pressure/hid-sensor-press.c | 19 ++++++-------------
 1 file changed, 6 insertions(+), 13 deletions(-)
drivers/iio/pressure/hid-sensor-press.c +6 -13
index 5f1d6abda3e4..a039b99d9851 100644
@@ -3,6 +3,7 @@
  * HID Sensors Driver
  * Copyright (c) 2014, Intel Corporation.
  */
+#include <linux/bitops.h>
 #include <linux/device.h>
 #include <linux/platform_device.h>
 #include <linux/module.h>
@@ -53,17 +54,6 @@ static const struct iio_chan_spec press_channels[] = {
 
 };
 
-/* Adjust channel real bits based on report descriptor */
-static void press_adjust_channel_bit_mask(struct iio_chan_spec *channels,
-					int channel, int size)
-{
-	channels[channel].scan_type.sign = 's';
-	/* Real storage bits will change based on the report desc. */
-	channels[channel].scan_type.realbits = size * 8;
-	/* Maximum size of a sample to capture is u32 */
-	channels[channel].scan_type.storagebits = sizeof(u32) * 8;
-}
-
 /* Channel read_raw handler */
 static int press_read_raw(struct iio_dev *indio_dev,
 			      struct iio_chan_spec const *chan,
@@ -225,8 +215,11 @@ static int press_parse_report(struct platform_device *pdev,
 			&st->press_attr);
 	if (ret < 0)
 		return ret;
-	press_adjust_channel_bit_mask(channels, CHANNEL_SCAN_INDEX_PRESSURE,
-					st->press_attr.size);
+	channels[CHANNEL_SCAN_INDEX_PRESSURE].scan_type = (struct iio_scan_type) {
+		.format = 's',
+		.realbits = BYTES_TO_BITS(st->press_attr.size),
+		.storagebits = BITS_PER_TYPE(u32),
+	};
 
 	dev_dbg(&pdev->dev, "press %x:%x\n", st->press_attr.index,
 			st->press_attr.report_id);
Signature · 1 line
2.51.0
#12 lore raw

Re: [PATCH v4 6/6] iio: pressure: HID: hid-sensor-press: Refactor channel initialization

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The commit introduces a compilation error by using the incorrect field name `.format` instead of…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The commit introduces a compilation error by using the incorrect field name .format instead of .sign when initializing struct iio_scan_type. --

commit 8d524ba8c95e026dee3af23dc0139fc49731c194
Author: Natália Salvino André <[email protected]>

iio: pressure: HID: hid-sensor-press: Refactor channel initialization

This commit replaces the local press_adjust_channel_bit_mask() function with a compound literal for scan_type initialization to improve code readability.

Quoted 4 lines
drivers/iio/pressure/hid-sensor-press.c +0 -0
index 5f1d6abda3e40..a039b99d9851d 100644

[ ... ]

drivers/iio/pressure/hid-sensor-press.c static int press_parse_report(struct platform_device *pdev, · line 219

Quoted 8 lines
drivers/iio/pressure/hid-sensor-press.c +2 -2
@@ -225,8 +215,11 @@ static int press_parse_report(struct platform_device *pdev,
 			&st->press_attr);
 	if (ret < 0)
 		return ret;
-	press_adjust_channel_bit_mask(channels, CHANNEL_SCAN_INDEX_PRESSURE,
-					st->press_attr.size);
+	channels[CHANNEL_SCAN_INDEX_PRESSURE].scan_type = (struct iio_scan_type) {
+		.format = 's',

[Severity: Low]
Does this introduce a build failure?

Looking at struct iio_scan_type defined in include/linux/iio/iio.h, the member used to indicate the sign of the sample is named sign, rather than format. The original code being removed correctly assigned to scan_type.sign.

Quoted 6 lines
drivers/iio/pressure/hid-sensor-press.c +3 -0
+		.realbits = BYTES_TO_BITS(st->press_attr.size),
+		.storagebits = BITS_PER_TYPE(u32),
+	};
 
 	dev_dbg(&pdev->dev, "press %x:%x\n", st->press_attr.index,
 			st->press_attr.report_id);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
maintainer
#13 lore raw
In reply to Natália Salvino André
Quoted 11 lines · On Tue, 19 May 2026 20:40:42 -0300 Natália Salvino André…

This series refactors the HID sensor drivers to standardize the initialization of the iio_chan_spec scan_type structure using compound literals.

This change improves code readability and ensures that all fields of the scan_type structure are properly zero-initialized, allowing the removal of local helper functions.

Additionally, the channel initialization loops for hid-sensor-accel-3d and hid-sensor-gyro-3d were cleaned up to iterate directly over the scan indices, eliminating redundant index-offset logic.

+CC Lixu

Please make sure to add to your +CC anyone who has been involve in earlier versions of the series.

I'm going to queue this now to get some build coverage. Still fine to add tags though or indeed drop it if any problems.

Applied to the testing branch of iio.git

thanks,

Jonathan

Quoted 47 lines

---

Changes in v4:

- Fixed a bug in the sensor_hub_input_get_attribute_info() call where passing the raw loop index 'ch' broke the HID report field lookup in hid-sensor-accel-3d. Restored the proper HID usage ID offset calculation using HID_USAGE_SENSOR_ACCEL_X_AXIS + ch
- Cleaned up the channel initialization loop to iterate directly over the scan indices in hid-sensor-gyro-3d for consistency

Changes in v3:

- Dropped the global helper function implementation from
hid-sensor-attributes.c following maintainer feedback.
- Shifted to local refactoring inside the drivers using compound
literals and the macros BYTES_TO_BITS() and BITS_PER_TYPE()
- Refactored the channel initialization loop in hid-sensor-accel-3d to
iterate directly over the scan enums instead of using 0-based
index offsets
- v3 link: https://lore.kernel.org/linux-iio/[email protected]/T/#t

Changes in v2:

- Modified the helper function to use the BITS_PER_BYTE macro instead of magic numbers for bit calculations
- Updated the scan_type structure field assignment from '.sign' to '.format' based on recent subsystem renaming feedback

Natália Salvino André (6):
iio: accel: HID: hid-sensor-accel-3d: Refactor channel initialization
iio: gyro: HID: hid-sensor-gyro-3d: Refactor channel initialization
iio: light: HID: hid-sensor-als: Refactor channel initialization
iio: light: HID: hid-sensor-prox: Refactor channel initialization
iio: magnetometer: HID: hid-sensor-magn-3d: Refactor channel
initialization
iio: pressure: HID: hid-sensor-press: Refactor channel initialization

 drivers/iio/accel/hid-sensor-accel-3d.c       | 27 +++++++------------
 drivers/iio/gyro/hid-sensor-gyro-3d.c         | 27 +++++++------------
 drivers/iio/light/hid-sensor-als.c            | 18 +++++--------
 drivers/iio/light/hid-sensor-prox.c           | 19 +++++--------
 drivers/iio/magnetometer/hid-sensor-magn-3d.c | 20 +++++---------
 drivers/iio/pressure/hid-sensor-press.c       | 19 +++++--------
 6 files changed, 42 insertions(+), 88 deletions(-)