From nobody Thu Apr 16 08:25:53 2026 Received: from mail-oo1-f48.google.com (mail-oo1-f48.google.com [209.85.161.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 51F41336EC5 for ; Sun, 1 Mar 2026 20:25:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772396733; cv=none; b=Fp7vF9gblb0qZCVp+5nVOPQ3PC4dyi4/zjxrvMOE5H9ddfVrrccxBnT9HXMPX4OUCFS9/7gbkqGCmPQeXN1PgpMUn/rZe0yihVK26eTBAHjvErOUn+HRa1MC7om6I98ftkTKxsLzPJLOR6/skDV7DtsgfL300qS3Y/dSUQi7yAI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772396733; c=relaxed/simple; bh=w79EgFPobXc1fNLX0IdgEikal4mapMbUm9s85WHfqNg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=pPVRRl2xsJTlnkcghYeL/OadSZ2UbQqRnn86vQ5BguJdv0eOGYZipoAfUSI4qThDnT9CiiufZTdRXld0avRsnBoNLKustO5j+irlsqx05iMFfIAu8c8oTjLdhtwwUk5Utl0NE22Bv29YO/VU4N8neZ4/+0GPBdFzfIqxOB5Ug/Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=3IlvLajq; arc=none smtp.client-ip=209.85.161.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="3IlvLajq" Received: by mail-oo1-f48.google.com with SMTP id 006d021491bc7-662efd1bdd4so2806581eaf.0 for ; Sun, 01 Mar 2026 12:25:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1772396731; x=1773001531; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=z4MOiR5mzBPo/Mx0DatRw9g2Ut/kzE5VHrbIV+pfL+0=; b=3IlvLajqAWppR3CK817paDIe65qdnkci0kq8svTYpJtK/c7YF5CcGR0QeoYT4n12oz VVOR0/XzqT2nK14APEjper/bC5LvqTpYdr9uyiKPpMTHuQQqMXIaOcx2K27GZ7E5WQvz /I7eZeeY51JtTkg3z65DRuI8frrfz5n65Omc0q7fwSeBfWCGb2PfFo0opiKOEX/eZhM2 GhJmTXQvayH0T33Cv8Pim1CAUkVAjGN1bRsRgdmzJpSWJS143JxGTSe95oWhYOzEis7A J3OUSChAPgEwNV254+7nsjpcz89sqDYK8cnvhN1Qhch7x245fnJ81vO53qyxHjEEXxGk x/Ng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772396731; x=1773001531; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=z4MOiR5mzBPo/Mx0DatRw9g2Ut/kzE5VHrbIV+pfL+0=; b=P1mJpWG19QlwXmcpj9k6juHixaZSG/rDAGt/QCt7iRJ8e696UlnmO9Eutho7yZGca5 2qq4qHI0ebTGCgJZh+Czc57QamGQcuzxjAWfbbdbGX7/e2gBDdkQTLVuyK9bwwvu/HQE CWoqhpSLICe2N5woWqwpQ8xRz4/kg8pECJGEPbb+go53k6TuzI0L6ld4IFJH/qJa2eHO JdsRpwgOqrMxY2heGfCi/myjEfouVHERgXNs5ENm7mmTtKAM1e09gZ2sb2VT1qq+pKGA 4sINc9HgrUC77Fb3OdI9MtUZg/TpjUCLTJDCmBpXzjhzfxiU9VJorymFeDho2GeD1MhQ 1QsQ== X-Forwarded-Encrypted: i=1; AJvYcCUm9wKK+SveCIrpWOllppFhYpyuM+qlS8ZkIpf4MD7by2Hk2dzteydjJKoCqt1/ftZRl9oGNkVHAtklkIk=@vger.kernel.org X-Gm-Message-State: AOJu0YyetPRv+9JGj/zv962O/6KFVRq3D03QSh7CfPhI+Euum63F9YYE 7PEP3Zbnkivo8vT7NY0Une1LCRsYKsVhKT6pMeBBYerA/4S9RVxQFJbZSW6y7w0FKcw= X-Gm-Gg: ATEYQzx6GK1JKaEgWUyBISZW7E/RYfAGLgCXNZLki44bLN5c5/J2XrW739QXDG7WZQg FwX3KshOmjfg7uCRvmqfUUfrvkK0h0nw0E4toHeZCPJ4qrubycmP9rIDtuBCaevNAUQSeSVW+L2 6Zl3uIK6iGk4rS6EwNAy/UjZiw6adEkSJqwohOQ9VB6cVhz9PaLJLTZt0sALhjLfwcoZ4FJ1V8d pZtsUmRDM/kZ2V470XcCie1L927O+HXnwvXjb+PA8VSymlQRNYlavCP36bvGMUkCCyPiYLusADn ouJxOYT8cFtTO8MkI27qo0V0gYKZJB+jPN1lKClwQFJNr8tnc+nvdJlizPk1sXphyOnpeyUQUtA OmW4psVqeguKC7pI98qCX2f4b+TbzPIk+2jtJuUb25t5yqBI3dAzYELZODGUIYsllm56B9u2IM9 Ku9h+42m0/cYHlgelslhwgvTpbXig/ X-Received: by 2002:a05:6820:4b08:b0:679:a41d:2d29 with SMTP id 006d021491bc7-679fb4394abmr5183163eaf.2.1772396731107; Sun, 01 Mar 2026 12:25:31 -0800 (PST) Received: from [127.0.1.1] ([2600:8803:e7e4:500:8b9c:d657:204d:5a5f]) by smtp.gmail.com with ESMTPSA id 586e51a60fabf-4160d277316sm10063025fac.17.2026.03.01.12.25.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Mar 2026 12:25:29 -0800 (PST) From: David Lechner Date: Sun, 01 Mar 2026 14:24:50 -0600 Subject: [PATCH 1/4] iio: orientation: hid-sensor-rotation: add timestamp hack to not break userspace Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260301-iio-fix-timestamp-alignment-v1-1-1a54980bfb90@baylibre.com> References: <20260301-iio-fix-timestamp-alignment-v1-0-1a54980bfb90@baylibre.com> In-Reply-To: <20260301-iio-fix-timestamp-alignment-v1-0-1a54980bfb90@baylibre.com> To: Jiri Kosina , Jonathan Cameron , Srinivas Pandruvada , =?utf-8?q?Nuno_S=C3=A1?= , Andy Shevchenko , =?utf-8?q?Lars_M=C3=B6llendorf?= , Lars-Peter Clausen , Greg Kroah-Hartman Cc: Jonathan Cameron , Lixu Zhang , linux-input@vger.kernel.org, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, David Lechner X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=10742; i=dlechner@baylibre.com; h=from:subject:message-id; bh=w79EgFPobXc1fNLX0IdgEikal4mapMbUm9s85WHfqNg=; b=owEBbQGS/pANAwAKAcLMIAH/AY/AAcsmYgBppKCc4+PtND/C7HPEVljZkQuAOkl8nLVneSj2k A6fg4oEkZ2JATMEAAEKAB0WIQTsGNmeYg6D1pzYaJjCzCAB/wGPwAUCaaSgnAAKCRDCzCAB/wGP wJFWCACNu0gJJ1bM8mp4u/nzubTieDtO+AXElboTgLhRUfkzhGoPv6Y7OHjQ3FvDXZlNiT6z9v2 IODRFHAPi2GefAmywSMCPxZXN9oA5wV/pE9rwSXb0LsoZPqq2rE9Jzvldt8X0D8tl1Qh67E5GfE rx9le4WnjBwtLlm/5fQQfj66ed4nPs+EV0ecDXb5OjtvsOqVaAh1mz/7E5Ft8+yH74Negu4KU3k SOQ4e43rLqkISyIbds3WC/JbsitELWlNKitReFio0x/vBfARvf7Sx6d2FsW+zHd0FYubG6IpTqv OAlDpwJPz3wXNRljR7yNAuMjh1Jc64Sj6CKYBG4zCZPJticc X-Developer-Key: i=dlechner@baylibre.com; a=openpgp; fpr=8A73D82A6A1F509907F373881F8AF88C82F77C03 Add a hack to push two timestamps in the hid-sensor-rotation scan data to avoid breaking userspace applications that depend on the timestamp being at the incorrect location in the scan data due to unintentional misalignment in older kernels. When this driver was written, the timestamp was in the correct location because of the way iio_compute_scan_bytes() was implemented at the time. (Samples were 24 bytes each.) Then commit 883f61653069 ("iio: buffer: align the size of scan bytes to size of the largest element") changed the computed scan_bytes to be a different size (32 bytes), which caused iio_push_to_buffers_with_timestamp() to place the timestamp at an incorrect offset. There have been long periods of time (6 years each) where the timestamp was in either location, so to not break either case, we open-code the timestamps to be pushed to both locations in the scan data. Reported-by: Jonathan Cameron Closes: https://lore.kernel.org/linux-iio/20260215162351.79f40b32@jic23-hua= wei/ Fixes: 883f61653069 ("iio: buffer: align the size of scan bytes to size of = the largest element") Signed-off-by: David Lechner --- I found that I could emulate this thanks to /dev/uhid. And thanks to AI code generators, I was able to reasonably quickly make a script that worked for emulating "HID-SENSOR-20008a". I'll include the script at the end. I set up the buffer like this: cd /sys/bus/iio/devices/iio:device1/buffer0 echo 1 > in_rot_quaternion_en echo 1 > in_timestamp_en echo 1 > enable Before this series is applied, we can see that the timestamp (group of 8 ending in "98 18") is at offset of 24 in the 32-byte data. hd /dev/iio\:device1 00000000 6a 18 00 00 ac f3 ff ff 83 2d 00 00 02 d3 ff ff |j........-....= ..| 00000010 00 00 00 00 00 00 00 00 5a 17 a0 2a 73 cb 98 18 |........Z..*s.= ..| 00000020 ad 17 00 00 6a f4 ff ff 35 2b 00 00 ca d0 ff ff |....j...5+....= ..| 00000030 00 00 00 00 00 00 00 00 2a a6 bb 30 73 cb 98 18 |........*..0s.= ..| 00000040 92 1e 00 00 50 ec ff ff ea c1 ff ff 78 f0 ff ff |....P.......x.= ..| 00000050 00 00 00 00 00 00 00 00 8f 3b a7 39 77 cb 98 18 |.........;.9w.= ..| After the first patch, we can see that the timestamp is now repeated at both the correct and previous incorrect offsets (24 and 32). (Normally, the last 8 bytes would be all 00 for padding.) 00000000 dd e0 ff ff 0e e0 ff ff 75 07 00 00 90 3f 00 00 |........u....?= ..| 00000010 f4 38 82 d0 3a cc 98 18 f4 38 82 d0 3a cc 98 18 |.8..:....8..:.= ..| 00000020 a0 e0 ff ff 1d e0 ff ff a0 0a 00 00 1c 3f 00 00 |.............?= ..| 00000030 3a 29 9f d6 3a cc 98 18 3a 29 9f d6 3a cc 98 18 |:)..:...:)..:.= ..| 00000040 a9 e1 ff ff 1e 14 00 00 6c c1 ff ff 98 f2 ff ff |........l.....= ..| 00000050 39 21 77 11 55 cc 98 18 39 21 77 11 55 cc 98 18 |9!w.U...9!w.U.= ..| Test script: import math import os import struct import time UHID_DESTROY =3D 1 UHID_CREATE2 =3D 11 UHID_INPUT2 =3D 12 UHID_DATA_MAX =3D 4096 BUS_USB =3D 0x03 class UHIDRotationSensor: def __init__(self, device_path: str =3D "/dev/uhid") -> None: """Initialize the virtual UHID rotation sensor. Args: device_path: Path to the UHID character device. """ self.device_path =3D device_path self.fd =3D -1 def open_device(self) -> None: """Open the UHID device.""" self.fd =3D os.open(self.device_path, os.O_RDWR) def close_device(self) -> None: """Close the UHID device.""" if self.fd >=3D 0: os.close(self.fd) self.fd =3D -1 def create_device(self) -> None: """Create and register a virtual HID rotation sensor.""" # HID descriptor for Sensor Device Orientation (HID-SENSOR-20008a) report_desc =3D bytes( [ 0x05, 0x20, # Usage Page: Sensor 0x09, 0x8A, # Usage: Device Orientation (0x20008A) 0xA1, 0x01, # Collection: Application # Input report (Report ID 1): quaternion x, y, z, w (s16) 0x85, 0x01, # Report ID 1 0x0A, 0x83, 0x04, # Usage: Orientation Quaternion (0x200483) 0x16, 0x00, 0x80, # Logical Minimum: -32768 0x26, 0xFF, 0x7F, # Logical Maximum: 32767 0x75, 0x10, # Report Size: 16 0x95, 0x04, # Report Count: 4 0x81, 0x02, # Input: Data, Variable, Absolute # Feature report (Report ID 2): report state, power state, # and report interval 0x85, 0x02, # Report ID 2 0x0A, 0x16, 0x03, # Usage: Property Report State (0x200316) 0x15, 0x01, # Logical Minimum: 1 0x25, 0x05, # Logical Maximum: 5 0x75, 0x08, 0x95, 0x01, 0xB1, 0x02, # Feature: Data, Variable, Absolute 0x0A, 0x19, 0x03, # Usage: Property Power State (0x200319) 0x15, 0x01, 0x25, 0x05, 0x75, 0x08, 0x95, 0x01, 0xB1, 0x02, # Feature: Data, Variable, Absolute 0x0A, 0x0E, 0x03, # Usage: Property Report Interval (0x20030E) 0x15, 0x00, # Logical Minimum: 0 0x27, 0xFF, 0xFF, 0xFF, 0x7F, # Logical Maximum: 2147483647 0x75, 0x20, # Report Size: 32 0x95, 0x01, # Report Count: 1 0xB1, 0x02, # Feature: Data, Variable, Absolute 0xC0, # End Collection ] ) # Build struct uhid_create2_req payload create_req_header =3D struct.pack( "<128s64s64sHHIIII", b"HID-SENSOR-20008a", # name b"AD Inc", # phys b"", # uniq len(report_desc), # rd_size BUS_USB, # bus 0x0001, # vendor 0x0001, # product 0, # version 0, # country ) create_req =3D ( create_req_header + report_desc + b"\x00" * (UHID_DATA_MAX - len(report_desc)) ) event =3D struct.pack(" Non= e: """Send one quaternion sample report. Args: qx: Quaternion X component (signed 16-bit). qy: Quaternion Y component (signed 16-bit). qz: Quaternion Z component (signed 16-bit). qw: Quaternion W component (signed 16-bit). """ report_id =3D 1 report =3D struct.pack( " None: """Run the virtual sensor simulation loop.""" try: self.open_device() self.create_device() time.sleep(0.5) angle =3D 0.0 while True: # Simulate changing orientation in quaternion form. half_angle =3D angle * 0.5 qx =3D int(8192 * math.sin(half_angle * 0.7)) qy =3D int(8192 * math.cos(half_angle * 0.5)) qz =3D int(16384 * math.sin(half_angle)) qw =3D int(16384 * math.cos(half_angle)) self.send_rotation_data(qx, qy, qz, qw) angle +=3D 0.1 time.sleep(0.1) except KeyboardInterrupt: print("\nStopping...") finally: self.close_device() if __name__ =3D=3D "__main__": sensor =3D UHIDRotationSensor() sensor.run() --- drivers/iio/orientation/hid-sensor-rotation.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/drivers/iio/orientation/hid-sensor-rotation.c b/drivers/iio/or= ientation/hid-sensor-rotation.c index e759f91a710a..a8bce5151dcb 100644 --- a/drivers/iio/orientation/hid-sensor-rotation.c +++ b/drivers/iio/orientation/hid-sensor-rotation.c @@ -20,7 +20,11 @@ struct dev_rot_state { struct hid_sensor_hub_attribute_info quaternion; struct { s32 sampled_vals[4]; - aligned_s64 timestamp; + /* + * HACK: There are two copies of the same timestamp in case of + * userspace depending on broken alignment from older kernels. + */ + aligned_s64 timestamp[2]; } scan; int scale_pre_decml; int scale_post_decml; @@ -154,8 +158,18 @@ static int dev_rot_proc_event(struct hid_sensor_hub_de= vice *hsdev, if (!rot_state->timestamp) rot_state->timestamp =3D iio_get_time_ns(indio_dev); =20 - iio_push_to_buffers_with_timestamp(indio_dev, &rot_state->scan, - rot_state->timestamp); + /* + * HACK: IIO previously had an incorrect implementation of + * iio_push_to_buffers_with_timestamp() that put the timestamp + * in the last 8 bytes of the buffer, which was incorrect + * according to the IIO ABI. To avoid breaking userspace that + * depended on this broken behavior, we put the timestamp in + * both the correct place and the old incorrect place. + */ + rot_state->scan.timestamp[0] =3D rot_state->timestamp; + rot_state->scan.timestamp[1] =3D rot_state->timestamp; + + iio_push_to_buffers(indio_dev, &rot_state->scan); =20 rot_state->timestamp =3D 0; } --=20 2.43.0 From nobody Thu Apr 16 08:25:53 2026 Received: from mail-oi1-f181.google.com (mail-oi1-f181.google.com [209.85.167.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EACF5337BB5 for ; Sun, 1 Mar 2026 20:25:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772396734; cv=none; b=AFCqfur3JZI65GyDa1QAzLUNSjeD2HWK4PQgLv+cmBu7mi65QkDZw8EdUx1FaYCPKZjxl1qo4EIZLBn1BwwYoGdHeuyZzyz/3+kRsOH9KxwjKnFGHutP4hrlJcRDAuW7bUWTT9MiKyV2UEcmJ/iE9FfCnuIO9gvtKXbenZGiGNc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772396734; c=relaxed/simple; bh=Df01JxrgdJ/czk7PscfActXgUF6VwomhAfkjAESPLys=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=htEdBmb66AIPjDr8ooD1TVjIDChCvPJf5O+cEGw5UqbwPHLED8BDQhUQ5we87biRnLQ/E1FZO5oJ0IfcMxuomliFJ5/fpChO5fLEkwPIHUUtdyud+bFUHLoOsq6nBG8HggbbRQoYDAhz7RKvJ+JrXZpV+5uwKiCKrci2HLhsnVE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=uhhhyiv1; arc=none smtp.client-ip=209.85.167.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="uhhhyiv1" Received: by mail-oi1-f181.google.com with SMTP id 5614622812f47-4638e6bb8a5so1489535b6e.0 for ; Sun, 01 Mar 2026 12:25:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1772396732; x=1773001532; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=bxzCN3wwvXWsW/jY59gqah99qTBhWFcny/0HrO0Lk6Q=; b=uhhhyiv1zR3AvFJL1XvTr3bCwy/VsKGzh7j9quJjgoaQmGIG+KDVOX1Rk9wYF7cZN6 hRdZPQZ986HLp1HZL7G4Q9hC4ukR+gQpryZcZ0ZxVsx0r97coFpKGZNXvJzBa+qd71UQ HmVq5DPTysFWOpRmkxa0AZy9N4yMuIb7gu3ueeeI5CpFBTrj5KBg1DdWkEHYCfvrSPsR S/0fF0rbXS8/qknDuC2o7OFY1Hklm74o65hpblLh2T5yvbzEM118TJqYT3QSd52TjDpa I1+h2anl+OShLyc+X0DDWJQFvGJrATGUqP04rza0APy4gQ26jq0dMsxtyGXpxqPl5iHk JmBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772396732; x=1773001532; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=bxzCN3wwvXWsW/jY59gqah99qTBhWFcny/0HrO0Lk6Q=; b=IXtPVdQbmqT8wGXcdBPM1TYgaIWzQLso6yCZRVS45H9kTvwXJqHBLAtCjA9wqXlFzW ClQqIUGZGhZo90HiAqOeQh63OyIia4i8daFSYZowmwjvg+ahIHw93Bem+CNzCBOLUJc1 FzkkgfE1LxqkblsDnuIuc3LanXIIbdk0+Jw25683mDZTTSYT2+DROPE1dyxXeO0vKLuy sl2ueOAH+RfJBef+fJ9sMrOLQAI0Hm9/jiXJEl+EJ2FgzB2JACdcT+8yxX7CkW6ObFby llnpIGQLyZftHwk8saSTTyvISADUD0pW7JnadtNxwviyfDe6sIm8juphgEKsF3Pcm2Uz kpDg== X-Forwarded-Encrypted: i=1; AJvYcCUk6ILLo6RwBNNmW4xQcei5wVptRBgar8lUPxVsmFPC0vTQYO7D0zmIhoQkIVbfhQhhVKPyyWwgC6DtXlY=@vger.kernel.org X-Gm-Message-State: AOJu0YxLZxuMyKYeygxsTS7w2p1uw2NkIV+Y+r8ErUWJFf7LLOTC3K/Q w/jcGn/+M9k/q+BGARW6GRsLtFmbd0uTyDd2wc83CanNhYx4cEbdheUHNJq4wk5NFUg= X-Gm-Gg: ATEYQzz8X7Tyzv5MHkuHS/OCo+BdSmGmCFmTLDZQ8G1DBNN3Safy0o8yrDXfIhWuqCR 9v3lYuqxRf4Kyq76PMcxn3/b9d3Df2R3ej1C2ty5p8bjJRjd12QCE1C4NLavUG4bj/XgYur0avu OE4ki+g24GDpJklX258Xx952RDWUmq2wvN9FuIP/6WmnB6wFyg7g65XHtFSvhGCv+HUwd1R97oT tw1A1jb8GmnIN6ge09ikBgwQWMwilj1cEBZFgdWFg7cV5y+zsEXeARW1Ngkw3GfxEHmROZaRlQ8 QOCxnX4Xlm1ooOb0yeUz+7twR+BjXlpiGXfKrmQaUrSbGGfa0tjMSkOXhqosWerNCN/kPw4ZtYp lvqWSQV9JYsPmo8/W8GT4Vi3FkQgrws8v4qwhKfMzx8bkr8Qnl5b6NaAvVnItvRas/gMucialH5 goOeypDq9UN6YvZoOt9i/aTmDprkA3 X-Received: by 2002:a05:6820:1885:b0:663:40d:48a3 with SMTP id 006d021491bc7-679fadb6ec3mr5760441eaf.8.1772396731996; Sun, 01 Mar 2026 12:25:31 -0800 (PST) Received: from [127.0.1.1] ([2600:8803:e7e4:500:8b9c:d657:204d:5a5f]) by smtp.gmail.com with ESMTPSA id 586e51a60fabf-4160d277316sm10063025fac.17.2026.03.01.12.25.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Mar 2026 12:25:31 -0800 (PST) From: David Lechner Date: Sun, 01 Mar 2026 14:24:51 -0600 Subject: [PATCH 2/4] iio: buffer: check return value of iio_compute_scan_bytes() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260301-iio-fix-timestamp-alignment-v1-2-1a54980bfb90@baylibre.com> References: <20260301-iio-fix-timestamp-alignment-v1-0-1a54980bfb90@baylibre.com> In-Reply-To: <20260301-iio-fix-timestamp-alignment-v1-0-1a54980bfb90@baylibre.com> To: Jiri Kosina , Jonathan Cameron , Srinivas Pandruvada , =?utf-8?q?Nuno_S=C3=A1?= , Andy Shevchenko , =?utf-8?q?Lars_M=C3=B6llendorf?= , Lars-Peter Clausen , Greg Kroah-Hartman Cc: Jonathan Cameron , Lixu Zhang , linux-input@vger.kernel.org, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, David Lechner X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=3313; i=dlechner@baylibre.com; h=from:subject:message-id; bh=Df01JxrgdJ/czk7PscfActXgUF6VwomhAfkjAESPLys=; b=owEBbQGS/pANAwAKAcLMIAH/AY/AAcsmYgBppKCjOvjbpvzjZFuFI5GjHBqD/VSIz93rBBcy2 iiKSDNP+wOJATMEAAEKAB0WIQTsGNmeYg6D1pzYaJjCzCAB/wGPwAUCaaSgowAKCRDCzCAB/wGP wJzVB/9NIIyQ7h1f9VkV7fjfHarpvVFFmMWtxU9CFevKWQR+gsbpXMCIWBIXW759ATP3yNS5p0M DWBav1gBpOgLTQhgUHhe3pRI4KTtr+e2akshFuQOUpxTB06IbY44JHzpDhiZkonZY8ZCJaa6CQQ YjEJga0dm+RcE8Qkxz2OBRva0T0B5ou24/eM9Q9BFGhECaaQYPkWHrAtb/JNrlzG9P1rFXWGs5x K8xwszcZyn1s3cdDOgY33Wm6CowSsBQy1oznbuWmCmMvtTUpHTsvCOpJFHmgNx5zk5MV1tmXDrB 9J6EjkagKZooY2N5HRBUMNlDJZPE194tajiCUOi8EiPLFHTE X-Developer-Key: i=dlechner@baylibre.com; a=openpgp; fpr=8A73D82A6A1F509907F373881F8AF88C82F77C03 Check return value of iio_compute_scan_bytes() as it can return an error. The result is moved to an output parameter while we are touching this as we will need to add a second output parameter in a later change. The return type of iio_buffer_update_bytes_per_datum() also had to be changed to propagate the error. Signed-off-by: David Lechner --- drivers/iio/industrialio-buffer.c | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-b= uffer.c index 46f36a6ed271..71dfc81cb9e5 100644 --- a/drivers/iio/industrialio-buffer.c +++ b/drivers/iio/industrialio-buffer.c @@ -764,7 +764,8 @@ static int iio_storage_bytes_for_timestamp(struct iio_d= ev *indio_dev) } =20 static int iio_compute_scan_bytes(struct iio_dev *indio_dev, - const unsigned long *mask, bool timestamp) + const unsigned long *mask, bool timestamp, + unsigned int *scan_bytes) { unsigned int bytes =3D 0; int length, i, largest =3D 0; @@ -790,8 +791,9 @@ static int iio_compute_scan_bytes(struct iio_dev *indio= _dev, largest =3D max(largest, length); } =20 - bytes =3D ALIGN(bytes, largest); - return bytes; + *scan_bytes =3D ALIGN(bytes, largest); + + return 0; } =20 static void iio_buffer_activate(struct iio_dev *indio_dev, @@ -836,18 +838,23 @@ static int iio_buffer_disable(struct iio_buffer *buff= er, return buffer->access->disable(buffer, indio_dev); } =20 -static void iio_buffer_update_bytes_per_datum(struct iio_dev *indio_dev, - struct iio_buffer *buffer) +static int iio_buffer_update_bytes_per_datum(struct iio_dev *indio_dev, + struct iio_buffer *buffer) { unsigned int bytes; + int ret; =20 if (!buffer->access->set_bytes_per_datum) - return; + return 0; =20 - bytes =3D iio_compute_scan_bytes(indio_dev, buffer->scan_mask, - buffer->scan_timestamp); + ret =3D iio_compute_scan_bytes(indio_dev, buffer->scan_mask, + buffer->scan_timestamp, &bytes); + if (ret) + return ret; =20 buffer->access->set_bytes_per_datum(buffer, bytes); + + return 0; } =20 static int iio_buffer_request_update(struct iio_dev *indio_dev, @@ -855,7 +862,10 @@ static int iio_buffer_request_update(struct iio_dev *i= ndio_dev, { int ret; =20 - iio_buffer_update_bytes_per_datum(indio_dev, buffer); + ret =3D iio_buffer_update_bytes_per_datum(indio_dev, buffer); + if (ret) + return ret; + if (buffer->access->request_update) { ret =3D buffer->access->request_update(buffer); if (ret) { @@ -898,6 +908,7 @@ static int iio_verify_update(struct iio_dev *indio_dev, struct iio_buffer *buffer; bool scan_timestamp; unsigned int modes; + int ret; =20 if (insert_buffer && bitmap_empty(insert_buffer->scan_mask, masklength)) { @@ -985,8 +996,11 @@ static int iio_verify_update(struct iio_dev *indio_dev, scan_mask =3D compound_mask; } =20 - config->scan_bytes =3D iio_compute_scan_bytes(indio_dev, - scan_mask, scan_timestamp); + ret =3D iio_compute_scan_bytes(indio_dev, scan_mask, scan_timestamp, + &config->scan_bytes); + if (ret) + return ret; + config->scan_mask =3D scan_mask; config->scan_timestamp =3D scan_timestamp; =20 --=20 2.43.0 From nobody Thu Apr 16 08:25:53 2026 Received: from mail-ot1-f42.google.com (mail-ot1-f42.google.com [209.85.210.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DE3B13382C3 for ; Sun, 1 Mar 2026 20:25:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772396736; cv=none; b=aP2rp9GKeVG36XvOWPGFZGbDZ7hDE1a3wXEuCIPGPzii4AZbpX/6/od9JDmq/ux+YWUTZZ5xxCUXg0RIGuv2R/wwPv3K10bUYxo/b0FUgdWf8udq9d9e1o5+bVSbUOz/GTKnfdaY+EwuwzLc1iymxGfcZV7rl27S6t5FqlcBa/M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772396736; c=relaxed/simple; bh=LcB+sMpnVNwP5Hxc7hd6TIyPDdQxqO0tl2qKUWZh7to=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=C7CEcHZxxbXTworfC7fua/Hujn/K0zf7Y/ZhYJ2DnCpgBqQsx77UCHJXKzNQwmw76AymtDuJYEcbWyNOGjwauhNYPqSX/kGUvGMsZtK9nX0A9fC2oPqM/r5tgeGZc9jX+L1mGj1I8vM3Km2od/S/aJ9q1I7q4+9EJP4JMCF51dE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=fGMqMVee; arc=none smtp.client-ip=209.85.210.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="fGMqMVee" Received: by mail-ot1-f42.google.com with SMTP id 46e09a7af769-7d513bc15c7so5215946a34.1 for ; Sun, 01 Mar 2026 12:25:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1772396734; x=1773001534; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=Vt0Qupd8WEsTD7s9mClwuAaT6mwAmhQXmzDPpUDpF44=; b=fGMqMVeetsKlc2x4zuspGaa481nIczOd7szWlq9DxO8/Q+1dn+CR2TdEE55fl6YJOV JvfO0qVIzI3Qj+z/yRk4KTbZ71XoQD7TDx/ffh2LRR9enHE0zsDGtzlOwIWHlgLMQWyF Kk6s5C0DIQpBgsFJVDJ3j2WtMM7hgesuqNfC+CFZjf5GCqh5r3x5w6d4tOeH1/ZBjx1a 7wOzPWm4c/kIqQmAQsA1Qsu7eNUvhEKBQRrh5qGWSPlXZWZDbjFctLoWzOEYJd3agDNN 5unboJeGBJUIysZCbCvjNnz5rUOfkxy5EdkXCX7v2Q6msFa2JPwWFsUVrRvbXHKlP+Sm b0Cw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772396734; x=1773001534; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=Vt0Qupd8WEsTD7s9mClwuAaT6mwAmhQXmzDPpUDpF44=; b=L5Bv2cQV7uLFxvwxf2P5wZ/rM5iHlnbmJvad4ntv3Hz3ioo4x49loZ5pEHWjtPf1jy ziMKcqbUd6qdLYlbrv+GGFfToJ6CRqmAbS61+jhLo5QE73TNr3AXYPbpA03QAZ/LSygf 6Gr1wvGbQEFCpRuYCFYFXcANznwQgBcDvIm5DO9Uo2Be96MH+h6XP0KbajyER9qSjlYI JOK783i5XjnxCia9umdQd8FfU55Ngp61vJddkhpiAkD/A8G6pz2hF+RHOD9GHMKEeFlM +xTiB1oYhmfnz38iPRK72O6T+f+qF/tXsXVXCsFq0j9pJiUu0aoz6ftcF6jk9k/doIRn F3RA== X-Forwarded-Encrypted: i=1; AJvYcCXZ5rBNM0utnznjzg4ZXODyzABJXQZk+1ykgTsJ82r+aQOU5zg0tm8o6CyEvFOhrhhNVT6cv0+m9tRLnqI=@vger.kernel.org X-Gm-Message-State: AOJu0YysP53NXNT4aHCrsFH5zXdGDf4Fo7twc+i4riQb3MBgUSNVEG5C mcnmZ+/MlGYpH7bNPZBsJU9z8TMyGZygOmvC7hE2xm5bktTthPhUuL7DFacTcOnGtDc= X-Gm-Gg: ATEYQzzlQX7NQpiyDCNIWvQyn5YHvTRFfSLpzyE5X9leLM9r0EnDFKfRu2gpPsE/KGS aQaqn+JSGoqX/b4rbcztiSWz2EFDuNYsKjKdb45PrEjzxhtTeD30pGoAfm5GEP7V3tdcCuQfuM9 hX0XGAlvWa3xw7lTJGmaAexPb6YF7uN9l9ZX15I17LqdN8LvpO+q2wtYvpq3pDzvnClZthI1QAH 13N4fDit8gBUmiunQrT6jT8gue/aXa3RJZcsL1PKRzC8RBCmFfsNWOg7/PRxzi1l68pPXmVu2Gy baZlrl1ED4G/A/J4U/C/itAFg3/4fNOt+FttRe6gFa+S5IfVjquT0H7dUb6RX4HorGVBxSq10Mq wSyntP0j4pd5VJlFQuRc4vllXzxJvD9H/K7/CZNqG0zyaryHGTsENBlRfVQTYWItwBD9nIqVXPF 44yYVNi1XfZhlI+V+EpgCNbVsTqn2k X-Received: by 2002:a05:6870:6c10:b0:3e7:ecd7:b292 with SMTP id 586e51a60fabf-41627000a52mr6907854fac.46.1772396733843; Sun, 01 Mar 2026 12:25:33 -0800 (PST) Received: from [127.0.1.1] ([2600:8803:e7e4:500:8b9c:d657:204d:5a5f]) by smtp.gmail.com with ESMTPSA id 586e51a60fabf-4160d277316sm10063025fac.17.2026.03.01.12.25.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Mar 2026 12:25:33 -0800 (PST) From: David Lechner Date: Sun, 01 Mar 2026 14:24:52 -0600 Subject: [PATCH 3/4] iio: buffer: cache largest scan element size Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260301-iio-fix-timestamp-alignment-v1-3-1a54980bfb90@baylibre.com> References: <20260301-iio-fix-timestamp-alignment-v1-0-1a54980bfb90@baylibre.com> In-Reply-To: <20260301-iio-fix-timestamp-alignment-v1-0-1a54980bfb90@baylibre.com> To: Jiri Kosina , Jonathan Cameron , Srinivas Pandruvada , =?utf-8?q?Nuno_S=C3=A1?= , Andy Shevchenko , =?utf-8?q?Lars_M=C3=B6llendorf?= , Lars-Peter Clausen , Greg Kroah-Hartman Cc: Jonathan Cameron , Lixu Zhang , linux-input@vger.kernel.org, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, David Lechner X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=3558; i=dlechner@baylibre.com; h=from:subject:message-id; bh=LcB+sMpnVNwP5Hxc7hd6TIyPDdQxqO0tl2qKUWZh7to=; b=owEBbQGS/pANAwAKAcLMIAH/AY/AAcsmYgBppKCpuTaFWQlUSMrljCvEf1iOJy7zpeU4zm/6/ EVY/RP6EVGJATMEAAEKAB0WIQTsGNmeYg6D1pzYaJjCzCAB/wGPwAUCaaSgqQAKCRDCzCAB/wGP wIH7B/9F3hxKaQC3jEziUe9nDueg18GlWretvM/l+UepH6LTejo+R5E57D16jcq4uVvCi7+di7U nVCShwaDddDOCqVD/ceUjexAVdx0C2nMZf+/nI11TJbi1tYsmHc0e4smX4rJ/vWq7xOi+BYiPYk oHBKEPxUXj6X5xSAofLicul8OjKXVYarGEVaBRW/m8xzeDWOz1oA/KzULAkHdYa/ULilsnhSBMe 7w10ohtxRbXFrkcVai97AoSQ1dCif4V0kRO4hDuMDzmuXJAUCQ1GazzGPd/gjJmBjUuEYAvneNF zY5WjVV1vhK02HtOmbarvU732qYKx471YUYqP0QkARXZHIgr X-Developer-Key: i=dlechner@baylibre.com; a=openpgp; fpr=8A73D82A6A1F509907F373881F8AF88C82F77C03 Cache the largest scan element size of elements enabled in a scan buffer. This will be used later to ensure proper alignment of the timestamp element in the scan buffer. The new field could not be placed in struct iio_dev_opaque because we will need to access it in a static inline function later, so we make it __private instead. It is only intended to be used by core IIO code. Signed-off-by: David Lechner --- drivers/iio/industrialio-buffer.c | 14 +++++++++++--- include/linux/iio/iio.h | 3 +++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-b= uffer.c index 71dfc81cb9e5..83e9392f949f 100644 --- a/drivers/iio/industrialio-buffer.c +++ b/drivers/iio/industrialio-buffer.c @@ -765,7 +765,8 @@ static int iio_storage_bytes_for_timestamp(struct iio_d= ev *indio_dev) =20 static int iio_compute_scan_bytes(struct iio_dev *indio_dev, const unsigned long *mask, bool timestamp, - unsigned int *scan_bytes) + unsigned int *scan_bytes, + unsigned int *largest_element_size) { unsigned int bytes =3D 0; int length, i, largest =3D 0; @@ -793,6 +794,9 @@ static int iio_compute_scan_bytes(struct iio_dev *indio= _dev, =20 *scan_bytes =3D ALIGN(bytes, largest); =20 + if (largest_element_size) + *largest_element_size =3D largest; + return 0; } =20 @@ -848,7 +852,7 @@ static int iio_buffer_update_bytes_per_datum(struct iio= _dev *indio_dev, return 0; =20 ret =3D iio_compute_scan_bytes(indio_dev, buffer->scan_mask, - buffer->scan_timestamp, &bytes); + buffer->scan_timestamp, &bytes, NULL); if (ret) return ret; =20 @@ -892,6 +896,7 @@ struct iio_device_config { unsigned int watermark; const unsigned long *scan_mask; unsigned int scan_bytes; + unsigned int largest_scan_element_size; bool scan_timestamp; }; =20 @@ -997,7 +1002,8 @@ static int iio_verify_update(struct iio_dev *indio_dev, } =20 ret =3D iio_compute_scan_bytes(indio_dev, scan_mask, scan_timestamp, - &config->scan_bytes); + &config->scan_bytes, + &config->largest_scan_element_size); if (ret) return ret; =20 @@ -1155,6 +1161,8 @@ static int iio_enable_buffers(struct iio_dev *indio_d= ev, indio_dev->active_scan_mask =3D config->scan_mask; ACCESS_PRIVATE(indio_dev, scan_timestamp) =3D config->scan_timestamp; indio_dev->scan_bytes =3D config->scan_bytes; + ACCESS_PRIVATE(indio_dev, largest_scan_element_size) =3D + config->largest_scan_element_size; iio_dev_opaque->currentmode =3D config->mode; =20 iio_update_demux(indio_dev); diff --git a/include/linux/iio/iio.h b/include/linux/iio/iio.h index a9ecff191bd9..85bcb5f8ae15 100644 --- a/include/linux/iio/iio.h +++ b/include/linux/iio/iio.h @@ -584,6 +584,8 @@ struct iio_buffer_setup_ops { * and owner * @buffer: [DRIVER] any buffer present * @scan_bytes: [INTERN] num bytes captured to be fed to buffer demux + * @largest_scan_element_size: [INTERN] cache of the largest scan element = size + * among the channels selected in the scan mask * @available_scan_masks: [DRIVER] optional array of allowed bitmasks. Sor= t the * array in order of preference, the most preferred * masks first. @@ -610,6 +612,7 @@ struct iio_dev { =20 struct iio_buffer *buffer; int scan_bytes; + unsigned int __private largest_scan_element_size; =20 const unsigned long *available_scan_masks; unsigned int __private masklength; --=20 2.43.0 From nobody Thu Apr 16 08:25:53 2026 Received: from mail-ot1-f48.google.com (mail-ot1-f48.google.com [209.85.210.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C3C8133A701 for ; Sun, 1 Mar 2026 20:25:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772396745; cv=none; b=AuluTV6R5QwnlJy9O+ilotreptcitP9XlcRm3OoZF1h2z3q9Qn01cb5HAqOuNga7zK0Tp9smxxf0Xv+gTH+GP5mIU9ObNYOGvfPMxBPCbF6snGQOOALevrGMUOlka9PW94RObxun/Wp7nQqJIX23tft6aqItwszqcjZx1ZVf/qg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772396745; c=relaxed/simple; bh=ps7ZUToN5AgC3idSqYVCwGGorlD/RoDfLukDDAMO5jY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=XcTZtxDaFnOmUHKaJnZ842eoxh3P2MlwmY0/aZM1eNs110Nov4POcs8319D55knsQSEboGn39etbDqze4bqde+i9or6NBmwgLW2IENhE4V7P8njLpHfS+d/fZh9i8+lN+5x2s3qjYDswBvyLVbayDTuGFtWwp0ucdbVax8FVoyo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=AHcPZkHw; arc=none smtp.client-ip=209.85.210.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="AHcPZkHw" Received: by mail-ot1-f48.google.com with SMTP id 46e09a7af769-7d598f4c618so1598658a34.0 for ; Sun, 01 Mar 2026 12:25:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1772396736; x=1773001536; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=eZodpChBQz3zA6o/aOiIaqgV1izpjgwL4nvy2nT3q2A=; b=AHcPZkHw+WhdQKjW2p817SsQLROvE08xKc4xEye1YmJNaFBoZWVvPg7WBP1QL+nzpX j1ABYA6xZ3IPzgnR3TGlwPWDqyvQwyGVVG9qOCiDUUAGdKuLm0UBlm2zMrI0t673pBcq X6C5glM076e1YeHXRVLgQY1+xMnuxE2tsJKB/KfQRw8XSUf63UuytsU6HmPE18MVsv3j voYMt/8Fo8i3X7Tdufc9AIoHZZ2Zdgzl/vBq5nV98nKNLY43BwuqxQkjhl153wW9N2g6 gcsQO09AsK5k/RMmHZeTvZYBfZz5/5VBGUb4dJuJwZr4bhsYK+D4t6fTFRAvfwbw/7fy FTGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772396736; x=1773001536; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=eZodpChBQz3zA6o/aOiIaqgV1izpjgwL4nvy2nT3q2A=; b=epdOvyvsmp/v9PzWKHcKWFT5qf6mkFlQY+0rnRXPxEkYuAwhBabBCo02xKg6RheP0u zJFGfsuoDACfoUpFgDf6wNUj+d/OEZNiKdP+O22t1ZIDD5INiuJYdc9mO9euKq8KAIfP 2RdCY5hBs2AtdLmQVA/4lO59WCkLD0VcQqx0fq1TiCkATQMDUoNWOjgHXSUhiKPQOPqe KiIOUZXxpdWvAn/YFoh9ZZ8NQP+JSIIjisoNGUp7Tth6rbS89cz8DfQ/CMmmfkGM6x9S WJCSz7/zDeJYkWPSYX7JZfj+RXu2LPViLQX6yhiU7vvdXhxkWfmOPJxL6t2BEcTyJCwS Ov5A== X-Forwarded-Encrypted: i=1; AJvYcCUfsqqwYYVgHkJ5qKZ6i3dycqA5BDr74iaRy17Rs0Q1Lg2j88nuDgoZMZXe1OAhhIqhVxm2FiVSSjv9SC8=@vger.kernel.org X-Gm-Message-State: AOJu0YxFrLHOA91w7iA7PoOlTR9At/U5NDMdDtoMF+MmEbJktlEGA4zs WHJSMxY0SXtefBQAeLHzVrnXdhJhDGzO6kQKYZQZFTrNzSkULVtMG373k9wHxlE2z/o= X-Gm-Gg: ATEYQzxIkKNJfwEBCmdWgcAnM5R4TPwzQY74ctY+8rQkXRD2KPojHLPSM7qhR8BiMKO con3wN3OgV//Kebq9+6BvDQZodh2rcUVqjfdwfIkebH0r5tMiJ7uKPtB8fGp75gRw0o5CPfMF+9 Iua3rWamjAov+RdvY1KoTUf5ECzblRXuGyi2d7qc2pKftlMmwRDKd5Z0/ZjQ3SzyKZ8mvBxLRas Ik2xx7AgDPHdRZhKR9s/i/K7r0JvVxCHr1UVeCmXtfeunMknGLOS4w9u/0kiz51aY1MNwl65yDS BYCsbho6uB7lt+zXgtyHpLC1hQ1QAAQge5MRTIzhjtJvkxK/EUW9k6Ye05IlNXPoPeWprFwUQ3p +FEEBAGPZ72JuiGBVLRqH/e3t2uz+KAZLX+hr4qYEq0eIbEFpYSntHXOZaPBpSO8INQHn8QpvT+ FomF02nxdhEwdqQ4EcmqYGDulL6VPv X-Received: by 2002:a05:6870:b2f2:b0:3e7:fa5f:7250 with SMTP id 586e51a60fabf-41626fb0924mr6857750fac.33.1772396735728; Sun, 01 Mar 2026 12:25:35 -0800 (PST) Received: from [127.0.1.1] ([2600:8803:e7e4:500:8b9c:d657:204d:5a5f]) by smtp.gmail.com with ESMTPSA id 586e51a60fabf-4160d277316sm10063025fac.17.2026.03.01.12.25.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Mar 2026 12:25:34 -0800 (PST) From: David Lechner Date: Sun, 01 Mar 2026 14:24:53 -0600 Subject: [PATCH 4/4] iio: buffer: fix timestamp alignment when quaternion in scan Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260301-iio-fix-timestamp-alignment-v1-4-1a54980bfb90@baylibre.com> References: <20260301-iio-fix-timestamp-alignment-v1-0-1a54980bfb90@baylibre.com> In-Reply-To: <20260301-iio-fix-timestamp-alignment-v1-0-1a54980bfb90@baylibre.com> To: Jiri Kosina , Jonathan Cameron , Srinivas Pandruvada , =?utf-8?q?Nuno_S=C3=A1?= , Andy Shevchenko , =?utf-8?q?Lars_M=C3=B6llendorf?= , Lars-Peter Clausen , Greg Kroah-Hartman Cc: Jonathan Cameron , Lixu Zhang , linux-input@vger.kernel.org, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, David Lechner X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=3132; i=dlechner@baylibre.com; h=from:subject:message-id; bh=ps7ZUToN5AgC3idSqYVCwGGorlD/RoDfLukDDAMO5jY=; b=owEBbQGS/pANAwAKAcLMIAH/AY/AAcsmYgBppKCwYE1zFgBlGDGu3K1oCQ/jPEYUUNKtvI1Ot 1PILepELrGJATMEAAEKAB0WIQTsGNmeYg6D1pzYaJjCzCAB/wGPwAUCaaSgsAAKCRDCzCAB/wGP wPs9CACHinHJ1VZdedi8872GMRPuqVtNXcLmjGgG84f0Hvo47puFDicij2+7ttc5/hNCkuucf0Q VxweBrvdIVjIgo+whzd5dGCz3jnBWwNbsiT9t8EuvxNQ37DQLwbvoeiA50ilhWodXv9zNqdwsda 6PcwvSMcYfoz7Gc0PtmmRphBtQErYG9exjFfCu3jkkc0g9VBx1lvJRGiX7XrezZB404lj5J9BeY POWRk0fM95AZ/+qFSv7Hjv1kBr8LrMzm8ptlHzYXku+Pk6kQvdISAXU4Q9TEOy9V0N2aQXb8GUK xDkChsUIJQ5XV3pqfKcHZRupjpZ8aijk967gxZjVAOOqiGk4 X-Developer-Key: i=dlechner@baylibre.com; a=openpgp; fpr=8A73D82A6A1F509907F373881F8AF88C82F77C03 Fix timestamp alignment when a scan buffer contains an element larger than sizeof(int64_t). Currently s32 quaternions are the only such element, and the one driver that has this (hid-sensor-rotation) has a workaround in place already so this change does not affect it. Previously, we assumed that the timestamp would always be 8-byte aligned relative to the end of the scan buffer, but in the case of a scan buffer a 16-byte quaternion vector, scan_bytes =3D=3D 32, but the timestamp needs to be placed at offset 16, not 24. Signed-off-by: David Lechner --- To test this, I used hid-sensor-rotation minus the first patch in the series so that we can see that the timestamp actually moved to the correct location. Before this patch, the timestamp (8 bytes ending with "98 18") is in the wrong location. 00000000 6a 18 00 00 ac f3 ff ff 83 2d 00 00 02 d3 ff ff |j........-....= ..| 00000010 00 00 00 00 00 00 00 00 5a 17 a0 2a 73 cb 98 18 |........Z..*s.= ..| 00000020 ad 17 00 00 6a f4 ff ff 35 2b 00 00 ca d0 ff ff |....j...5+....= ..| 00000030 00 00 00 00 00 00 00 00 2a a6 bb 30 73 cb 98 18 |........*..0s.= ..| 00000040 92 1e 00 00 50 ec ff ff ea c1 ff ff 78 f0 ff ff |....P.......x.= ..| 00000050 00 00 00 00 00 00 00 00 8f 3b a7 39 77 cb 98 18 |.........;.9w.= ..| After this patch, timestamp is now in the correct location. 00000000 55 0f 00 00 dd 1f 00 00 af 0b 00 00 ec 3e 00 00 |U............>= ..| 00000010 c7 17 68 42 6d d0 98 18 00 00 00 00 00 00 00 00 |..hBm.........= ..| 00000020 57 0e 00 00 c8 1f 00 00 d1 0e 00 00 42 3e 00 00 |W...........B>= ..| 00000030 56 a2 87 48 6d d0 98 18 00 00 00 00 00 00 00 00 |V..Hm.........= ..| 00000040 a3 e2 ff ff d3 1b 00 00 0b c9 ff ff cc 20 00 00 |............. = ..| 00000050 27 59 4d b3 72 d0 98 18 00 00 00 00 00 00 00 00 |'YM.r.........= ..| I also tested this with a different driver not affected by this bug to make sure that the timestamp is still in the correct location for all other drivers. --- include/linux/iio/buffer.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/include/linux/iio/buffer.h b/include/linux/iio/buffer.h index d37f82678f71..ac19b39bdbe4 100644 --- a/include/linux/iio/buffer.h +++ b/include/linux/iio/buffer.h @@ -34,8 +34,16 @@ static inline int iio_push_to_buffers_with_timestamp(str= uct iio_dev *indio_dev, void *data, int64_t timestamp) { if (ACCESS_PRIVATE(indio_dev, scan_timestamp)) { - size_t ts_offset =3D indio_dev->scan_bytes / sizeof(int64_t) - 1; - ((int64_t *)data)[ts_offset] =3D timestamp; + size_t ts_offset =3D indio_dev->scan_bytes - + ACCESS_PRIVATE(indio_dev, largest_scan_element_size); + + /* + * The size of indio_dev->scan_bytes is always aligned to + * largest_scan_element_size (see iio_compute_scan_bytes()). + * And this size is always going to be >=3D sizeof(timestamp). + * So to correctly place the timestamp, it goes at this offset. + */ + *(int64_t *)(data + ts_offset) =3D timestamp; } =20 return iio_push_to_buffers(indio_dev, data); --=20 2.43.0