From 3ad2c290c7464774be1afd567f6f7ac3b2137be3 Mon Sep 17 00:00:00 2001 From: Lucas Goossen Date: Sun, 25 Oct 2026 17:18:30 -0300 Subject: [PATCH] soc: apple: sep: keep a held J313 patch and its data-ready line On a MacBook Air (M1, 2020), J313, the first verify after wake registers the Touch ID sensor twice. Attach on a 13.5 profile wakes and patches the sensor and does not calibrate, so that first verify power-cycles, calibrates, then power-cycles and registers again. Later verifies register once, because each one switches the sensor off. Remember whether this driver left the sensor powered. A completed registration is kept only when the sensor is still powered, the status is not silent, the patch acknowledgement is accepted and the state is not NEEDS_PATCH. A power cycle is what clears a stuck state 9, so that state still forces a reload even when the acknowledgement reads accepted. After calibration, register again only when that status no longer holds. A nonce-session profile never takes the second wake. The same boot polls for capture. The J313 platform tree names an interrupt on pin 104 with trigger flags 3. That value is IRQ_TYPE_EDGE_BOTH in Linux and level-low in the Apple GPIO block, and either one re-fires the capture loop. The driver asks for a rising edge, which is what J314s, J316s and J414 already describe, and iBoot left the pin as a plain input with a pull-down and no trigger. Name pinctrl_ap as the parent: spi2's parent is the AIC, so a bare interrupts property would be parsed as an AIC interrupt. Configure the line once the sensor is patched and idle. A board that does not describe it still falls back to SPI polling. Not booted with this change. Signed-off-by: Lucas Goossen --- arch/arm64/boot/dts/apple/t8103-j313.dts | 20 ++-- drivers/soc/apple/registration.rs | 59 ++++++++++ drivers/soc/apple/sbio.rs | 130 ++++++++++++++++++----- drivers/soc/apple/sensor.rs | 21 +++- 4 files changed, 197 insertions(+), 33 deletions(-) diff --git a/arch/arm64/boot/dts/apple/t8103-j313.dts b/arch/arm64/boot/dts/apple/t8103-j313.dts index 299227ea2b9a0b..05ecb65aded2f2 100644 --- a/arch/arm64/boot/dts/apple/t8103-j313.dts +++ b/arch/arm64/boot/dts/apple/t8103-j313.dts @@ -95,12 +95,18 @@ }; /* - * Mesa fingerprint sensor on SPI2. The power line (pin 108, active high) comes - * from the J313 platform device tree, which also lists an interrupt on pin 104. The - * interrupt is left out on purpose: the driver polls, and the trigger flags in - * the ADT (3) are not decoded with any certainty here. Pin 104 reads back as a - * plain input with a pull-down, which rules out the two active-low readings of - * that value but does not decide between the remaining ones. + * Mesa fingerprint sensor on SPI2. Power is pin 108, active high, from the + * J313 platform device tree. That tree also lists an interrupt on pin 104 + * with trigger flags 3. Linux IRQ_TYPE_EDGE_BOTH is the value 3, and + * both-edges re-fires when the line falls and spins the capture loop. Apple + * GPIO mode 3 is level-low. The pin register saved on 2026-10-21 reads + * mode 0, pull-down, input enabled: iBoot left a plain input and programmed + * no trigger. Data-ready is the rising edge. The driver requests + * IRQF_TRIGGER_RISING, and J314s, J316s and J414 describe this line as + * IRQ_TYPE_EDGE_RISING. t8103.dtsi already includes irq.h. + * + * spi2 names the AIC as its interrupt parent. This node names pinctrl_ap, + * whose interrupt cells are , so pin 104 is a GPIO interrupt. * * Do not add cs-gpios. The sensor needs the controller's native chip select, * otherwise the SPI core skips the hardware CS timing. @@ -112,6 +118,8 @@ &mesa { status = "okay"; enable-gpios = <&pinctrl_ap 108 GPIO_ACTIVE_HIGH>; + interrupt-parent = <&pinctrl_ap>; + interrupts = <104 IRQ_TYPE_EDGE_RISING>; spi-cs-setup-delay-ns = <20>; spi-cs-hold-delay-ns = <20>; /* diff --git a/drivers/soc/apple/registration.rs b/drivers/soc/apple/registration.rs index a49b75e42488fd..3d6b263809aeef 100644 --- a/drivers/soc/apple/registration.rs +++ b/drivers/soc/apple/registration.rs @@ -60,6 +60,37 @@ pub(crate) fn patch_loaded(nonce_session: bool, accepted: bool, needs_patch: boo accepted && !(nonce_session && needs_patch) } +/// Whether a completed registration can be left in place. +/// +/// Stricter than [`patch_loaded`] on a 13.5 profile: state 9 forces a reload +/// even when the acknowledgement still reads accepted, because a power cycle +/// is what clears a stuck NEEDS_PATCH. `left_powered` is this driver's record +/// that it has not switched the sensor off since that registration. +pub(crate) fn registration_still_held( + established: bool, + left_powered: bool, + silent: bool, + accepted: bool, + needs_patch: bool, +) -> bool { + established && left_powered && !silent && accepted && !needs_patch +} + +/// Whether calibration dropped the patch, so a profile without a nonce +/// session has to power-cycle and register again. A nonce-session profile +/// never takes that second wake: its parameter path does not. +pub(crate) fn needs_reload_after_calibration( + nonce_session: bool, + silent: bool, + accepted: bool, + needs_patch: bool, +) -> bool { + if nonce_session { + return false; + } + !registration_still_held(true, true, silent, accepted, needs_patch) +} + /// The bring-up stage retry `retry` (counted from 1) starts from. /// /// The stage an attempt left stands: once the serial is registered, [`plan`] @@ -365,6 +396,34 @@ mod tests { ); } + #[test] + fn a_held_registration_needs_every_condition() { + assert!(registration_still_held(true, true, false, true, false)); + assert!(!registration_still_held(false, true, false, true, false)); + assert!(!registration_still_held(true, false, false, true, false)); + assert!(!registration_still_held(true, true, true, true, false)); + assert!(!registration_still_held(true, true, false, false, false)); + assert!(!registration_still_held(true, true, false, true, true)); + } + + #[test] + fn calibration_skips_the_second_wake_while_the_patch_remains() { + assert!(!needs_reload_after_calibration(false, false, true, false)); + } + + #[test] + fn calibration_reloads_when_the_patch_is_gone() { + assert!(needs_reload_after_calibration(false, false, true, true)); + assert!(needs_reload_after_calibration(false, true, true, false)); + assert!(needs_reload_after_calibration(false, false, false, false)); + } + + #[test] + fn a_nonce_session_never_takes_the_post_calibration_wake() { + assert!(!needs_reload_after_calibration(true, true, false, true)); + assert!(!needs_reload_after_calibration(true, false, true, false)); + } + /// The enclave's sensor registration, as the boots on record show it. #[derive(Clone, Copy, Debug, PartialEq, Eq)] enum Enclave { diff --git a/drivers/soc/apple/sbio.rs b/drivers/soc/apple/sbio.rs index 9c69ca184c6912..a9893efb2828c6 100644 --- a/drivers/soc/apple/sbio.rs +++ b/drivers/soc/apple/sbio.rs @@ -1107,8 +1107,9 @@ impl SepData { registration::bring_up(retry, &mut attempts) } - /// One attach-time bring-up. A profile without a nonce session only - /// wakes and patches the sensor here; its first verify calibrates it. + /// One attach-time bring-up. A profile without a nonce session wakes and + /// patches the sensor here, and configures the data-ready line once that + /// patch is in place. Its first verify calibrates it. fn attach_bring_sensor_online(&self) -> bool { if self.profile.nonce_session { return self.bring_sensor_online(); @@ -1125,6 +1126,9 @@ impl SepData { } let online = self.complete_bringup(patch); diag::sensor_brought_up(bind, online); + if online { + self.configure_data_ready(); + } online } @@ -1281,9 +1285,63 @@ impl SepData { self.late_attach_wanted.xchg(false, Relaxed) } + /// Configures the data-ready line once. The sensor is patched and idle + /// here: reconfiguring the line mid-capture drops the firmware patch, so + /// a later call returns once the line is available. A board that does not + /// describe the line falls back to SPI polling. + fn configure_data_ready(&self) { + if sensor::irq_available() { + return; + } + if sensor::irq_setup() { + dev_info!(self.dev, "sensor: interrupt-driven capture\n"); + } else { + dev_warn!( + self.dev, + "sensor: no data-ready interrupt (describe it in DT); capture falls back to SPI polling\n" + ); + } + } + + /// Whether a 13.5 registration still holds a patched sensor this driver + /// left powered, so the next bring-up can skip the power cycle. A + /// nonce-session profile, a sensor this driver switched off, and a + /// registration that never completed return false without an SPI read. + /// State 9 forces a reload even when the acknowledgement is still accepted. + fn sensor_patch_held(&self) -> bool { + if self.profile.nonce_session + || !sensor::left_powered() + || self.bringup.load(Relaxed) < BRINGUP_ESTABLISHED + { + return false; + } + let Ok(st) = sensor::status() else { + return false; + }; + registration::registration_still_held( + true, + true, + st.is_silent(), + st.patch_ack() == sensor::PATCH_ACCEPTED, + st.state == sensor::STATE_NEEDS_PATCH, + ) + } + fn bring_sensor_online(&self) -> bool { let bind = diag::sensor_bind(); - let Some(mut patch) = self.wake_sensor() else { + let patch_held = self.sensor_patch_held(); + if patch_held { + dev_info!( + self.dev, + "sensor: patch still acknowledged; keeping the registration\n" + ); + } + let patch_now = if patch_held { + Some(PatchLoaded(())) + } else { + self.wake_sensor() + }; + let Some(mut patch) = patch_now else { diag::sensor_brought_up(bind, false); return false; }; @@ -1306,11 +1364,43 @@ impl SepData { } self.sensor_calibrated.store(true, Relaxed); if parameters.is_none() { - let Some(reloaded_patch) = self.wake_sensor() else { - diag::sensor_brought_up(bind, false); - return false; + let reload = match sensor::status() { + Err(error) => { + dev_warn!( + self.dev, + "sensor: status read after calibration failed ({:?}); registering again\n", + error + ); + true + } + Ok(st) => { + let again = registration::needs_reload_after_calibration( + self.profile.nonce_session, + st.is_silent(), + st.patch_ack() == sensor::PATCH_ACCEPTED, + st.state == sensor::STATE_NEEDS_PATCH, + ); + if again { + dev_info!( + self.dev, + "sensor: status after calibration requires a new registration\n" + ); + } else { + dev_info!( + self.dev, + "sensor: calibration left the patch in place; not registering again\n" + ); + } + again + } }; - patch = reloaded_patch; + if reload { + let Some(reloaded_patch) = self.wake_sensor() else { + diag::sensor_brought_up(bind, false); + return false; + }; + patch = reloaded_patch; + } } } @@ -1323,19 +1413,8 @@ impl SepData { None => self.complete_bringup(patch), }; diag::sensor_brought_up(bind, ok); - // The sensor is patched and idle here -- the only safe moment to - // configure the data-ready interrupt, which is then left alone for the - // driver's life. Interrupt capture is the default; a machine that does - // not describe a data-ready line falls back to SPI polling. - if ok && !sensor::irq_available() { - if sensor::irq_setup() { - dev_info!(self.dev, "sensor: interrupt-driven capture\n"); - } else { - dev_warn!( - self.dev, - "sensor: no data-ready interrupt (describe it in DT); capture falls back to SPI polling\n" - ); - } + if ok { + self.configure_data_ready(); } ok } @@ -1562,11 +1641,12 @@ impl SepData { && bio::verify_is_live(&self.bio_session.lock(), self.capture_epoch.load(Relaxed)) } - // Every verify registers the sensor again. On a profile with a nonce - // session that registration can fail as the attach-time one does, most - // often the first one after a system suspend, and a new attempt recovers - // it in the same way. Without one, the failure ends the user's - // authentication even though the sensor works. + // A nonce-session verify registers again. That registration can fail the + // way the attach-time one does, most often the first one after suspend, + // and a new attempt recovers it. Without a nonce session a failed + // bring-up ends the authentication. A 13.5 verify keeps a registration + // whose patch is still acknowledged, and power-cycles when this driver + // has switched the sensor off or the status no longer shows the patch. fn bring_sensor_online_for_verify(&self) -> bool { if !self.profile.nonce_session { return self.bring_sensor_online(); diff --git a/drivers/soc/apple/sensor.rs b/drivers/soc/apple/sensor.rs index c0f3a6159f74c9..63dbc5901fe798 100644 --- a/drivers/soc/apple/sensor.rs +++ b/drivers/soc/apple/sensor.rs @@ -214,9 +214,16 @@ pub(crate) fn power_line() -> Option { } } +/// True after this driver switched the sensor on, and not since switched it +/// off. A failed power cycle stores false: a half-finished cycle is not a +/// sensor a status read may trust. +static LEFT_POWERED: Atomic = Atomic::new(false); + pub(crate) fn power_cycle() -> bool { // SAFETY: the shim holds the descriptor and applies its delays. - unsafe { sep_sensor_power_cycle() == 0 } + let ok = unsafe { sep_sensor_power_cycle() == 0 }; + LEFT_POWERED.store(ok, Relaxed); + ok } pub(crate) fn cs_timing() -> CsTiming { @@ -231,7 +238,17 @@ pub(crate) fn power_source() -> PowerSource { pub(crate) fn power(on: bool) -> bool { // SAFETY: the shim holds the descriptor and applies its delays. - unsafe { sep_sensor_power(if on { 1 } else { 0 }) == 0 } + let ok = unsafe { sep_sensor_power(if on { 1 } else { 0 }) == 0 }; + if ok { + LEFT_POWERED.store(on, Relaxed); + } + ok +} + +/// Whether this driver left the sensor powered. Callers use it so they do not +/// issue an SPI status read to a sensor they have themselves switched off. +pub(crate) fn left_powered() -> bool { + LEFT_POWERED.load(Relaxed) } /// Sets up the data-ready interrupt once, while the sensor is idle. Returns