iio: hid-sensor: als: scale each channel individually
Subsystems (3)
PATCH 1/1 iio: hid-sensor: als: scale each channel individually
Some sensors have multiple channels (not just brightness but color ALS sensors) with different scaling factors.
Tested on Asus ProArt PX13 laptop by Sven.
--- drivers/iio/light/hid-sensor-als.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-)
drivers/iio/light/hid-sensor-als.c +12 -10
@@ -33,9 +33,9 @@ struct als_state {u32 illum[CHANNEL_SCAN_INDEX_MAX ];aligned_s64 timestamp; } scan; -int scale_pre_decml; -int scale_post_decml; -int scale_precision; +int scale_pre_decml[CHANNEL_SCAN_INDEX_MAX ]; +int scale_post_decml[CHANNEL_SCAN_INDEX_MAX ]; +int scale_precision[CHANNEL_SCAN_INDEX_MAX ];int value_offset;int num_channels;s64 timestamp;
@@ -186,9 +186,11 @@ static int als_read_raw(struct iio_dev *indio_dev, ret_type =IIO_VAL_INT ;break ;case IIO_CHAN_INFO_SCALE : - *val = als_state->scale_pre_decml; - *val2 = als_state->scale_post_decml; - ret_type = als_state->scale_precision; +if (chan->scan_index >=CHANNEL_SCAN_INDEX_MAX ) +return -EINVAL ; + *val = als_state->scale_pre_decml[chan->scan_index]; + *val2 = als_state->scale_post_decml[chan->scan_index]; + ret_type = als_state->scale_precision[chan->scan_index];break ;case IIO_CHAN_INFO_OFFSET : *val = als_state->value_offset;
@@ -338,6 +340,10 @@ static int als_parse_report(struct platform_device *pdev,als_adjust_channel_bit_mask (channels, index, st->als[i].size); ++index; + st->scale_precision[i] =hid_sensor_format_scale (usage_id, + &st->als[i], &st->scale_pre_decml[i], + &st->scale_post_decml[i]); +dev_dbg (&pdev->dev,"als %x:%x\n" , st->als[i].index, st->als[i].report_id); }
@@ -347,10 +353,6 @@ static int als_parse_report(struct platform_device *pdev,if (index) ret =0 ; - st->scale_precision =hid_sensor_format_scale (usage_id, - &st->als[CHANNEL_SCAN_INDEX_INTENSITY ], - &st->scale_pre_decml, &st->scale_post_decml); -return ret; }
---
base-commit: 194a48576843858afcd9c00ed4f105ec2b398e6c
change-id: 20260723-chromaticity-88fec417f00c
Best regards,
Signature · 1 line
Daniel Schaefer <[email protected]>
[email protected]
AI reviewThank 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:
- [Medium] The patch fails to expose the individual scale for the CHROMATICITY_Y channel because it still uses info_mask_shared_by_type.
Pre-existing issues:
- [High] The driver's IIO buffer layout uses a fixed structure, but available_scan_masks is dynamically populated, causing misaligned buffer reads and data corruption. --
--- Patch [1]: [PATCH] iio: hid-sensor: als: scale each channel individually --- commit 6d49cfd4cf380845a584116e06dcf23fc8c25ad6
Author: Daniel Schaefer <[email protected]>
iio: hid-sensor: als: scale each channel individually
This commit updates the hid-sensor-als driver to support individual scaling factors for each channel, instead of using a single global scale. It stores the scale attributes in arrays indexed by channel.
drivers/iio/light/hid-sensor-als.c struct als_state { · line 35
Quoted 8 lines
drivers/iio/light/hid-sensor-als.c +0
-0@@ -33,9 +33,9 @@ struct als_state {u32 illum[CHANNEL_SCAN_INDEX_MAX ];aligned_s64 timestamp; } scan;
[Severity: High]
This is a pre-existing issue, but does this fixed struct buffer layout cause data corruption?
The driver captures samples in als_capture_sample() into a static sparse array (als_state->scan.illum) based on fixed channel indices. However, indio_dev->available_scan_masks is dynamically constructed based on the attributes present on the sensor.
The IIO core calculates scan_bytes and channel offsets assuming the buffer is tightly packed.
When iio_push_to_buffers_with_ts() is called in als_proc_event(), it writes the timestamp at the end of the tightly packed length, which overwrites valid channel data in the sparse array.
The IIO demuxer then reads from incorrect offsets, scrambling the data sent to userspace.
Should the driver pack the samples sequentially instead of using fixed index offsets?
Quoted 9 lines
drivers/iio/light/hid-sensor-als.c +3
-3-int scale_pre_decml; -int scale_post_decml; -int scale_precision; +int scale_pre_decml[CHANNEL_SCAN_INDEX_MAX ]; +int scale_post_decml[CHANNEL_SCAN_INDEX_MAX ]; +int scale_precision[CHANNEL_SCAN_INDEX_MAX ];int value_offset;int num_channels;s64 timestamp;
[ ... ]
drivers/iio/light/hid-sensor-als.c static int als_read_raw(struct iio_dev *indio_dev, · line 194
Quoted 13 lines
drivers/iio/light/hid-sensor-als.c +5
-3@@ -186,9 +186,11 @@ static int als_read_raw(struct iio_dev *indio_dev, ret_type =IIO_VAL_INT ;break ;case IIO_CHAN_INFO_SCALE : - *val = als_state->scale_pre_decml; - *val2 = als_state->scale_post_decml; - ret_type = als_state->scale_precision; +if (chan->scan_index >=CHANNEL_SCAN_INDEX_MAX ) +return -EINVAL ; + *val = als_state->scale_pre_decml[chan->scan_index]; + *val2 = als_state->scale_post_decml[chan->scan_index]; + ret_type = als_state->scale_precision[chan->scan_index];break ;
[Severity: Medium]
Since the patch introduces per-channel scaling factors, can userspace actually read the scale for the CHROMATICITY_Y channel?
The als_channels array defines both CHANNEL_SCAN_INDEX_CHROMATICITY_X and Y with .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE).
Because of this, the IIO core will create a single in_chromaticity_scale sysfs attribute for both.
When read, IIO queries the first registered channel of that type (X), leaving the Y scale factor completely inaccessible.
Does the info_mask_shared_by_type for these channels need to be changed to info_mask_separate so userspace can read the newly added scale for CHROMATICITY_Y?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 1 line
Does the
info_mask_shared_by_typefor these channels need to be changed to
info_mask_separate so userspace can read the newly added scale for CHROMATICITY_Y?
Yes, that's a good idea. Will send v2 after I tested it.
Quoted 98 lines · On Thu, Jul 23, 2026 at 1:35 AM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
New issues:
- [Medium] The patch fails to expose the individual scale for theCHROMATICITY_Ychannel because it still usesinfo_mask_shared_by_type.Pre-existing issues:
- [High] The driver's IIO buffer layout uses a fixed structure, butavailable_scan_masksis dynamically populated, causing misaligned buffer reads and data corruption. ----- Patch [1]: [PATCH] iio: hid-sensor: als: scale each channel individually --- commit
6d49cfd4cf380845a584116e06dcf23fc8c25ad6
Author: Daniel Schaefer <[email protected]>iio: hid-sensor: als: scale each channel individually
This commit updates the hid-sensor-als driver to support individual scaling factors for each channel, instead of using a single global scale. It stores the scale attributes in arrays indexed by channel.
drivers/iio/light/hid-sensor-als.c struct als_state { · line 35
Quoted 8 lines
drivers/iio/light/hid-sensor-als.c +0
-0@@ -33,9 +33,9 @@ struct als_state {u32 illum[CHANNEL_SCAN_INDEX_MAX ];aligned_s64 timestamp; } scan;[Severity: High]
This is a pre-existing issue, but does this fixed struct buffer layout cause data corruption?The driver captures samples in
als_capture_sample()into a static sparse array (als_state->scan.illum) based on fixed channel indices. However,indio_dev->available_scan_masksis dynamically constructed based on the attributes present on the sensor.The IIO core calculates
scan_bytesand channel offsets assuming the buffer is tightly packed.When
iio_push_to_buffers_with_ts()is called inals_proc_event(), it writes the timestamp at the end of the tightly packed length, which overwrites valid channel data in the sparse array.The IIO demuxer then reads from incorrect offsets, scrambling the data sent to userspace.
Should the driver pack the samples sequentially instead of using fixed index offsets?
Quoted 9 lines
drivers/iio/light/hid-sensor-als.c +3
-3-int scale_pre_decml; -int scale_post_decml; -int scale_precision; +int scale_pre_decml[CHANNEL_SCAN_INDEX_MAX ]; +int scale_post_decml[CHANNEL_SCAN_INDEX_MAX ]; +int scale_precision[CHANNEL_SCAN_INDEX_MAX ];int value_offset;int num_channels;s64 timestamp;[ ... ]
drivers/iio/light/hid-sensor-als.c static int als_read_raw(struct iio_dev *indio_dev, · line 194
Quoted 13 lines
drivers/iio/light/hid-sensor-als.c +5
-3@@ -186,9 +186,11 @@ static int als_read_raw(struct iio_dev *indio_dev, ret_type =IIO_VAL_INT ;break ;case IIO_CHAN_INFO_SCALE : - *val = als_state->scale_pre_decml; - *val2 = als_state->scale_post_decml; - ret_type = als_state->scale_precision; +if (chan->scan_index >=CHANNEL_SCAN_INDEX_MAX ) +return -EINVAL ; + *val = als_state->scale_pre_decml[chan->scan_index]; + *val2 = als_state->scale_post_decml[chan->scan_index]; + ret_type = als_state->scale_precision[chan->scan_index];break ;[Severity: Medium]
Since the patch introduces per-channel scaling factors, can userspace actually read the scale for theCHROMATICITY_Ychannel?The
als_channelsarray defines bothCHANNEL_SCAN_INDEX_CHROMATICITY_Xand Y with .info_mask_shared_by_type =BIT(IIO_CHAN_INFO_SCALE).Because of this, the IIO core will create a single
in_chromaticity_scalesysfs attribute for both.When read, IIO queries the first registered channel of that type (X), leaving the Y scale factor completely inaccessible.
Does the
info_mask_shared_by_typefor these channels need to be changed toinfo_mask_separateso userspace can read the newly added scale forCHROMATICITY_Y?--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Jonathan Cameron
maintainerQuoted 6 lines · On Thu, 23 Jul 2026 01:18:58 +0800 Daniel Schaefer via B4 Relay…
From: Daniel Schaefer <[email protected]>
Some sensors have multiple channels (not just brightness but color ALS sensors) with different scaling factors.
Tested on Asus ProArt PX13 laptop by Sven.
The channels in this driver report IIO_CHAN_INFO_SCALE as shared by type. That doesn't matter unless we get different scales on the two parts of chromaticity and I'm assuming that isn't eh issue Sven has run into?
Assuming it is illuminance vs color temp (or both chromaticity channels together) then the change here looks fine to me.
I'll queue it up on the testing branch of iio.git but more eyes welcome if anyone has time to take a look.
A few little tweaks to the commit message. There was a bit of fuzz as well whilst applying but all seemed like line changes due to other updates.
Thanks
Jonathan
Quoted 4 lines
Signed-off-by Daniel Schaefer <[email protected]>Cc Sven Vainküla <[email protected]>
Given you say Sven tested it, I've upgraded this to a Tested-by and included a comment after that to say what it was tested on.
Quoted 9 lines
Cc Jiri Kosina <[email protected]>Cc Jonathan Cameron <[email protected]>Cc Srinivas Pandruvada <[email protected]>Cc David Lechner <[email protected]>Cc Nuno Sá <[email protected]>Cc Andy Shevchenko <[email protected]>
We don't really want to end up with all this list in the git log, so good practice is to put a --- above them, so they end up in the region that is cut out.
Quoted 64 lines
--- drivers/iio/light/hid-sensor-als.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-)drivers/iio/light/hid-sensor-als.c +12
-11@@ -33,9 +33,9 @@ struct als_state {u32 illum[CHANNEL_SCAN_INDEX_MAX ];aligned_s64 timestamp; } scan; -int scale_pre_decml; -int scale_post_decml; -int scale_precision; +int scale_pre_decml[CHANNEL_SCAN_INDEX_MAX ]; +int scale_post_decml[CHANNEL_SCAN_INDEX_MAX ]; +int scale_precision[CHANNEL_SCAN_INDEX_MAX ];int value_offset;int num_channels;s64 timestamp;@@ -186,9 +186,11 @@ static int als_read_raw(struct iio_dev *indio_dev, ret_type =IIO_VAL_INT ;break ;case IIO_CHAN_INFO_SCALE : - *val = als_state->scale_pre_decml; - *val2 = als_state->scale_post_decml; - ret_type = als_state->scale_precision; +if (chan->scan_index >=CHANNEL_SCAN_INDEX_MAX ) +return -EINVAL ; + *val = als_state->scale_pre_decml[chan->scan_index]; + *val2 = als_state->scale_post_decml[chan->scan_index]; + ret_type = als_state->scale_precision[chan->scan_index];break ;case IIO_CHAN_INFO_OFFSET : *val = als_state->value_offset;@@ -338,6 +340,10 @@ static int als_parse_report(struct platform_device *pdev,als_adjust_channel_bit_mask (channels, index, st->als[i].size); ++index; + st->scale_precision[i] =hid_sensor_format_scale (usage_id, + &st->als[i], &st->scale_pre_decml[i], + &st->scale_post_decml[i]); +dev_dbg (&pdev->dev,"als %x:%x\n" , st->als[i].index, st->als[i].report_id); }@@ -347,10 +353,6 @@ static int als_parse_report(struct platform_device *pdev,if (index) ret =0 ; - st->scale_precision =hid_sensor_format_scale (usage_id, - &st->als[CHANNEL_SCAN_INDEX_INTENSITY ], - &st->scale_pre_decml, &st->scale_post_decml); -return ret; } ---base-commit:
194a48576843858afcd9c00ed4f105ec2b398e6c
change-id: 20260723-chromaticity-88fec417f00cBest regards,
Quoted 9 lines · On Thu, 2026-07-23 at 01:18 +0800, Daniel Schaefer via B4 Relay wrote:
From: Daniel Schaefer <[email protected]>
Some sensors have multiple channels (not just brightness but color ALS
sensors) with different scaling factors.Tested on Asus ProArt PX13 laptop by Sven.
Signed-off-by Daniel Schaefer <[email protected]>
Quoted 12 lines
Link:
https://gitlab.freedesktop.org/hadess/iio-sensor-proxy/-/merge_requests/413
Cc: Sven Vainküla <[email protected]>
Cc: Jiri Kosina <[email protected]>
Cc: Jonathan Cameron <[email protected]>
Cc: Srinivas Pandruvada <[email protected]>
Cc: David Lechner <[email protected]>
Cc: Nuno Sá <[email protected]>
Cc: Andy Shevchenko <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Quoted 75 lines
---
drivers/iio/light/hid-sensor-als.c | 22 ++++++++++++----------1 file changed, 12 insertions(+), 10 deletions(-)drivers/iio/light/hid-sensor-als.c +3
-3@@ -33,9 +33,9 @@ struct als_state { u32 illum[CHANNEL_SCAN_INDEX_MAX]; aligned_s64 timestamp; } scan; -int scale_pre_decml; -int scale_post_decml; -int scale_precision; +int scale_pre_decml[CHANNEL_SCAN_INDEX_MAX ]; +int scale_post_decml[CHANNEL_SCAN_INDEX_MAX ]; +int scale_precision[CHANNEL_SCAN_INDEX_MAX ]; int value_offset; int num_channels; s64 timestamp;@@ -186,9 +186,11 @@ static int als_read_raw(struct iio_dev *indio_dev, ret_type = IIO_VAL_INT; break;case
IIO_CHAN_INFO_SCALE:drivers/iio/light/hid-sensor-als.c +4
-3- *val = als_state->scale_pre_decml; - *val2 = als_state->scale_post_decml; - ret_type = als_state->scale_precision; +if (chan->scan_index >=CHANNEL_SCAN_INDEX_MAX ) +return -EINVAL ; + *val = als_state->scale_pre_decml[chan->scan_index]; + *val2 = als_state->scale_post_decml[chan-Quoted 1 line
scan_index];drivers/iio/light/hid-sensor-als.c +1
-0+ ret_type = als_state->scale_precision[chan-Quoted 1 line
scan_index];break;
caseIIO_CHAN_INFO_OFFSET:
*val =als_state->value_offset;drivers/iio/light/hid-sensor-als.c +0
-0@@ -338,6 +340,10 @@ static int als_parse_report(struct platform_device *pdev,als_adjust_channel_bit_mask(channels, index, st-
Quoted 1 line
als[i].size);
++index;
drivers/iio/light/hid-sensor-als.c +1
-0+ st->scale_precision[i] =hid_sensor_format_scale(usage_id,
drivers/iio/light/hid-sensor-als.c +1
-0+ &st->als[i], &st-Quoted 1 line
scale_pre_decml[i],
drivers/iio/light/hid-sensor-als.c +2
-0+ &st->scale_post_decml[i]); +
dev_dbg(&pdev->dev, "als %x:%x\n", st->als[i].index, st->als[i].report_id);
}drivers/iio/light/hid-sensor-als.c +0
-0@@ -347,10 +353,6 @@ static int als_parse_report(structplatform_device *pdev, if (index) ret = 0;drivers/iio/light/hid-sensor-als.c +0
-2- st->scale_precision = hid_sensor_format_scale(usage_id, - &st-Quoted 1 line
als[CHANNEL_SCAN_INDEX_INTENSITY],
drivers/iio/light/hid-sensor-als.c +0
-1- &st->scale_pre_decml, &st-Quoted 1 line
scale_post_decml);-
return ret;
}---
base-commit:194a48576843858afcd9c00ed4f105ec2b398e6c
change-id: 20260723-chromaticity-88fec417f00cBest regards,
Jonathan Cameron
maintainerQuoted 23 lines · On Mon, 27 Jul 2026 08:18:47 -0700 srinivas pandruvada…
Quoted 9 lines · On Thu, 2026-07-23 at 01:18 +0800, Daniel Schaefer via B4 Relay wrote:
From: Daniel Schaefer <[email protected]>
Some sensors have multiple channels (not just brightness but color ALS
sensors) with different scaling factors.Tested on Asus ProArt PX13 laptop by Sven.
Signed-off-by Daniel Schaefer <[email protected]>Quoted 12 lines
Link:
https://gitlab.freedesktop.org/hadess/iio-sensor-proxy/-/merge_requests/413
Cc: Sven Vainküla <[email protected]>
Cc: Jiri Kosina <[email protected]>
Cc: Jonathan Cameron <[email protected]>
Cc: Srinivas Pandruvada <[email protected]>
Cc: David Lechner <[email protected]>
Cc: Nuno Sá <[email protected]>
Cc: Andy Shevchenko <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Quoted 1 line
Added. Thanks Srinivas!
J
Quoted 77 lines
Quoted 76 lines
---
drivers/iio/light/hid-sensor-als.c | 22 ++++++++++++----------1 file changed, 12 insertions(+), 10 deletions(-)drivers/iio/light/hid-sensor-als.c +3
-3@@ -33,9 +33,9 @@ struct als_state { u32 illum[CHANNEL_SCAN_INDEX_MAX]; aligned_s64 timestamp; } scan; -int scale_pre_decml; -int scale_post_decml; -int scale_precision; +int scale_pre_decml[CHANNEL_SCAN_INDEX_MAX ]; +int scale_post_decml[CHANNEL_SCAN_INDEX_MAX ]; +int scale_precision[CHANNEL_SCAN_INDEX_MAX ]; int value_offset; int num_channels; s64 timestamp;@@ -186,9 +186,11 @@ static int als_read_raw(struct iio_dev *indio_dev, ret_type = IIO_VAL_INT; break;case
IIO_CHAN_INFO_SCALE:drivers/iio/light/hid-sensor-als.c +4
-3- *val = als_state->scale_pre_decml; - *val2 = als_state->scale_post_decml; - ret_type = als_state->scale_precision; +if (chan->scan_index >=CHANNEL_SCAN_INDEX_MAX ) +return -EINVAL ; + *val = als_state->scale_pre_decml[chan->scan_index]; + *val2 = als_state->scale_post_decml[chan-Quoted 1 line
scan_index];drivers/iio/light/hid-sensor-als.c +1
-0+ ret_type = als_state->scale_precision[chan-Quoted 1 line
scan_index];break;
caseIIO_CHAN_INFO_OFFSET:
*val =als_state->value_offset;drivers/iio/light/hid-sensor-als.c +0
-0@@ -338,6 +340,10 @@ static int als_parse_report(struct platform_device *pdev,als_adjust_channel_bit_mask(channels, index, st-
Quoted 1 line
als[i].size);
++index;
drivers/iio/light/hid-sensor-als.c +1
-0+ st->scale_precision[i] =hid_sensor_format_scale(usage_id,
drivers/iio/light/hid-sensor-als.c +1
-0+ &st->als[i], &st-Quoted 1 line
scale_pre_decml[i],
drivers/iio/light/hid-sensor-als.c +2
-0+ &st->scale_post_decml[i]); +
dev_dbg(&pdev->dev, "als %x:%x\n", st->als[i].index, st->als[i].report_id);
}drivers/iio/light/hid-sensor-als.c +0
-0@@ -347,10 +353,6 @@ static int als_parse_report(structplatform_device *pdev, if (index) ret = 0;drivers/iio/light/hid-sensor-als.c +0
-2- st->scale_precision = hid_sensor_format_scale(usage_id, - &st-Quoted 1 line
als[CHANNEL_SCAN_INDEX_INTENSITY],
drivers/iio/light/hid-sensor-als.c +0
-1- &st->scale_pre_decml, &st-Quoted 1 line
scale_post_decml);-
return ret;
}---
base-commit:194a48576843858afcd9c00ed4f105ec2b398e6c
change-id: 20260723-chromaticity-88fec417f00cBest regards,
Quoted 3 lines
The channels in this driver report
IIO_CHAN_INFO_SCALEas shared by type. That doesn't matter unless we get different scales on the two parts of chromaticity and I'm assuming that isn't eh issue Sven has run into?
Yes, in practice it's probably not needed and the patch works as is.
Quoted 1 line
I'll queue it up on the testing branch of iio.git but more eyes welcome if
anyone has time to take a look.
Sorry I'm not sure what that means. Is the patch applied or should I send another one with commit message update?
David Lechner
reviewerQuoted 5 lines · Daniel Schaefer
Quoted 1 line
I'll queue it up on the testing branch of iio.git but more eyes welcome if
anyone has time to take a look.
Sorry I'm not sure what that means. Is the patch applied or should I send another one with commit message update?
It means that it is accepted/applied.
The iio/testing branch is the first step where various CI bots will do some automatic checks. If they find something, you will be notified. Otherwise nothing more for you to do (unless a serious concern is raised later).
And it means there is still time for further review too in case anyone wants to add their e.g. reviewed-by tag.