Re: [PATCH v7 1/2] i2c: core: Add i2c_update_timeout() helper for dynamic transfer timeouts

From: Aniket RANDIVE

Date: Mon Aug 24 2026 - 06:07:11 EST




On 8/24/2026 11:49 AM, Mukesh Savaliya wrote:


On 8/13/2026 11:15 AM, Aniket Randive wrote:
[...]

+void i2c_update_timeout(struct i2c_adapter *adap, u32 bus_freq_hz,
+            size_t len, unsigned int safety_coeff,
+            unsigned int min_usec)
+{
+    u64 bit_usec = mul_u64_u32_div(len * 9, USEC_PER_SEC, bus_freq_hz);
let's validate bus_freq_hz to prevent from 0 value in case of any abnormal caller.

This scenario is highly unlikely. Most drivers operate with a default bus frequency, typically 400 kHz, and therefore the condition being checked is not expected to occur in practice. As a result, I do not believe this validation is necessary in this case.

Thanks,
Aniket


+    u64 total_usec = bit_usec * safety_coeff + min_usec;
+    unsigned long jiffies_val;
+
+    /* Userspace-configured timeout always takes precedence. */
+    if (adap->user_timeout > 0) {
+        adap->timeout = adap->user_timeout;
+        return;
+    }
+
+    jiffies_val = usecs_to_jiffies((unsigned int)min_t(u64, total_usec, UINT_MAX));
+    /* adap->timeout is int; guard against signed overflow. */
+    adap->timeout = (int)min_t(unsigned long, jiffies_val, INT_MAX);
+}
+EXPORT_SYMBOL_GPL(i2c_update_timeout);
+#endif /* CONFIG_I2C_DYNAMIC_TIMEOUT */
+

[...]