From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=185.67.36.66 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796146894; b=pgqJX5tsEKSsxGznIMS0O/BCwDJLZnYHrG6dMR0Nk6WMTBz5kb7eMiC3qP5fdWevZ7yq VZcTXalrH/CEavAW8N5j3zSnDEtn6XRmfSPDFrniHOwlZgUvuodNXRQhPmv8ch/vuvpGR BCuveOTEwuvt+K4gFmSaR500cvvygtO1H7KfllyK2FGIaO8W3lxRz43O5Ma2uSEkx7AOw /eQOi0HifFk49trPTW0NYd7tOcYqsSXNxAlMRzwOJeiVVqpXeK8+gjfrDhg6G76bOeYLN FCnLU+RswN2YlFeNJJC7WyCVfGyEAdB6jpSP0tM+uDWncQGP98kSWAXWAcNEVW/cJxg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796146894; h=DMARC-Filter:Received:DKIM-Signature:Received:Message-ID:Subject: From:To:Cc:Date:In-Reply-To:References:Autocrypt:Content-Type: MIME-Version:OpenPGP; bh=Zih3pc8t/XugWBDLgkYXlR8i5uT08nCiMAhGY8iS8+I=; b=uy9zswNKxp4VVB0QSXC413zqUSVo6/CW1nFT9bt4PezHzwC3MYUJIUO8RMhaVyzO4P5W BDWOaBpGnIxo55bGshj77ms+dohA47bWYJRSI3r23pvjbAaUn9YGIFwwJV9UcKPZ9cf/o 27FsqRekEdsYYtLZ/n0tw8ozZyQ9ct+a6xObt8ghMnBCM2jwxx/P1EvEr+kn/Y644adxK Y1tOHWsOIfYY6XM8gT5/JgoHVk48Ggvq76oG+ft0V2NFsHLYgCqlCmNEB8JVCG+Suci6H CPuW1lmbKMPtfbYAM5VmXaaj+qNPFC4LT2JD+KdQYOCx3YfZVkxnCsVOptjKxlfcrKw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=posteo.de; spf=pass smtp.mailfrom=posteo.de; dkim=pass header.d=posteo.de header.i=@posteo.de header.a=rsa-sha256 header.s=1984.8680eb header.b=TNIvmIzS; arc=none smtp.remote-ip=185.67.36.66 Received: from mout02.posteo.de (mout02.posteo.de [185.67.36.66]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E1AED606A336 for ; Tue, 1 Dec 2026 17:41:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E1AED606A336 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=posteo.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=posteo.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=posteo.de header.i=@posteo.de header.a=rsa-sha256 header.s=1984.8680eb header.b=TNIvmIzS Received: from submission (posteo.de [185.67.36.169]) by mout02.posteo.de (Postfix) with ESMTPS id 151B0240103 for ; Tue, 1 Dec 2026 18:41:31 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=posteo.de; s=1984.8680eb; t=1796146891; bh=Zih3pc8t/XugWBDLgkYXlR8i5uT08nCiMAhGY8iS8+I=; h=Message-ID:Subject:From:To:Cc:Date:Autocrypt:Content-Type: MIME-Version:OpenPGP:From; b=TNIvmIzS5k+JXlv+f9Kml1sk93EXvbTqN5qfi+fCojbSN8G64dQc/+6P5guAOnHZI tCcKsa3OExdqwNNyUqFirbJTDYwYg2hnMxl4IcdM3Imzb8DHj247R9gx06PK/7Y1iV Ea4YJgWIg9UN2snvRMk4Iv+7SeTW2hojdwc5tRQz9HQs5EYqIFzZIuQyDvgEfV+usu AYLz34g9b2odHd9PLwlxNH3ONik+mlkZu8zUOv0f9n1HxvDyxuWrSN7RVGF44TKLQK 8yNFdNFgKi0UyEp0qeU24Vn0b2aKF/q25uQhwvJWnzJm1HVtOY0X54Mfw0reU/P6xD 7iM0/hVLLTg5Q== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4jf7VH5TMpz9rxD; Tue, 1 Dec 2026 18:41:27 +0100 (CET) Message-ID: <1e83eaf5024e0da010cb9f7d118303f4c848740b.camel@posteo.de> Subject: Re: [RFC PATCH v8 2/4] i2c: rust: implement SMBus access via IoBackend From: Markus Probst To: Muchamad Coirul Anwar , Jonathan Cameron , Miguel Ojeda , Igor Korotin , Danilo Krummrich Cc: David Lechner , Nuno =?ISO-8859-1?Q?S=E1?= , Andy Shevchenko , Andi Shyti , Daniel Almeida , Alice Ryhl , Mark Brown , Joshua Crofts , Shuai Liu <847807867@qq.com>, linux-iio@vger.kernel.org, linux-i2c@vger.kernel.org, rust-for-linux@vger.kernel.org, driver-core@lists.linux.dev, linux-kernel@vger.kernel.org Date: Tue, 01 Dec 2026 17:41:30 +0000 In-Reply-To: <20261201165508.39336-3-muchamadcoirulanwar@gmail.com> References: <20261201165508.39336-1-muchamadcoirulanwar@gmail.com> <20261201165508.39336-3-muchamadcoirulanwar@gmail.com> Autocrypt: addr=markus.probst@posteo.de; prefer-encrypt=mutual; keydata=mQINBGiDvXgBEADAXUceKafpl46S35UmDh2wRvvx+UfZbcTjeQOlSwKP7YVJ4JOZrVs93 qReNLkOWguIqPBxR9blQ4nyYrqSCV+MMw/3ifyXIm6Pw2YRUDg+WTEOjTixRCoWDgUj1nOsvJ9tVA m76Ww+/pAnepVRafMID0rqEfD9oGv1YrfpeFJhyE2zUw3SyyNLIKWD6QeLRhKQRbSnsXhGLFBXCqt 9k5JARhgQof9zvztcCVlT5KVvuyfC4H+HzeGmu9201BVyihJwKdcKPq+n/aY5FUVxNTgtI9f8wIbm fAjaoT1pjXSp+dszakA98fhONM98pOq723o/1ZGMZukyXFfsDGtA3BB79HoopHKujLGWAGskzClwT jRQxBqxh/U/lL1pc+0xPWikTNCmtziCOvv0KA0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KCRA0dh/4561D0gKJD/9uOQKYlsDoQX65Gd0LiMT0C+5vXgr3VI0PHDOwcv 51fJ3A1vNyPZRFPGrz8+mDEXUQOF/INfnz5Tu1QHwf+iYcWcTGAN/FHgVR6ET6VBNU2hJaKhu+Ggo kjYyJTOvyX+3yNRUfSny0GjTjIPuPTErjqmHF+BtjXslpgwqnNMznf3lRIuUjRORupos6p3k1DndE 5vzUTmXSvMyXyOD2KhBl/kL76k0bHYyAQytZPag12pltrtFbA/r2phDGN2si8PooDT99bSTJjaM45 MTAAHbHKJfvgfK41bNFD5mMtpWpL195XRtS0Nrxdg3PaYBxN5gtTG0RyZfpYRlkdEhm+jj/8RxuSG i/qdhRdbiI7K2IELWeQVHSNDi9JabR/UzlR4NSnhfAjRIVlRM+eFbUl8XwxwVrAkojF5IraH2qRvg VCmuFsHUW07FUlrDrzpjXsD73cKppoFGDCdDR0BHJepXbFLS9+AqkT+guRJlnCTg2p+TQtnbwPgKp Vj98JixovCl99zRYTsL2bRNU5+q8iET65VMJ1ydyNanvLd5vI/NqDkXhlXLsGmdaDTtu4R21PkToX dQNGrZ91M9nlIBKw8Y7c7xZ4098qX2b8JX/CxD+gC1r4C8vuA3GkhFLx+KlkON7LyiJPkrePp6Qky jfGillcaQOqFZ3WwVqyzG1BUfTow== Content-Type: multipart/signed; micalg="pgp-sha256"; protocol="application/pgp-signature"; boundary="=-LFzGMxqsNpbYQl5YFmIm" X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 OpenPGP: url=https://posteo.de/keys/markus.probst@posteo.de.asc; preference=encrypt --=-LFzGMxqsNpbYQl5YFmIm Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Tue, 2026-12-01 at 23:55 +0700, Muchamad Coirul Anwar wrote: > Implement fallible SMBus byte and word access for bound I2C clients > using the generic I/O backend infrastructure. >=20 > I2C transactions may fail due to NACK, arbitration loss, or timeout, so > use FallibleIoCapable rather than the infallible IoCapable interface. > Propagate errors returned by the I2C core to callers. >=20 > Provide smbus_io() for byte-addressed register access and standalone > word helpers for layouts not represented by the Region-based view. > Also provide smbus_read_word_swapped() for devices that transmit the > high byte of a multi-byte value first. >=20 > Restrict these operations to I2cClient, ensuring that bus I/O is > only available while the client remains bound, and implement Sync for > I2cClient since its exposed SMBus operations serialize through the > I2C core bus lock. >=20 > Suggested-by: Shuai Liu <847807867@qq.com> > Link: https://lore.kernel.org/all/tencent_173B7731E90B9624F9F82613DC3031B= 70907@qq.com/ > Suggested-by: Markus Probst > Link: https://lore.kernel.org/all/947615fd284c416891ac6de5d16cdd00ec5267a= b.camel@posteo.de/ > Signed-off-by: Muchamad Coirul Anwar > --- > Igor reviewed this patch in v7, but since I added `Sync` on > `I2cClient` and updated some docs afterward, I did not > carry his `Reviewed-by:` tag forward. Another review would be > appreciated. >=20 > rust/kernel/i2c.rs | 243 ++++++++++++++++++++++++++++++++++++++++++++- > 1 file changed, 242 insertions(+), 1 deletion(-) >=20 > diff --git a/rust/kernel/i2c.rs b/rust/kernel/i2c.rs > index b4ff25c830fc..25a8fa2f6329 100644 > --- a/rust/kernel/i2c.rs > +++ b/rust/kernel/i2c.rs > @@ -13,8 +13,15 @@ > devres::Devres, > driver, > error::*, > + io::{ > + FallibleIoCapable, > + IoBackend, > + IoBase, > + Region, // > + }, > of, > prelude::*, > + ptr::KnownSize, > sync::aref::{ > ARef, > AlwaysRefCounted, // > @@ -520,9 +527,15 @@ fn as_ref(&self) -> &device::Device { > unsafe impl Send for I2cClient {} > =20 > // SAFETY: `I2cClient` can be shared among threads because all methods o= f `I2cClient` > -// (i.e. `I2cClient) are thread safe. > +// (i.e. `I2cClient`) are thread safe. > unsafe impl Sync for I2cClient {} > =20 > +// SAFETY: `I2cClient` exposes only thread-safe `&self` operation= s: > +// SMBus transfers serialize via the I2C core bus lock and read only imm= utable > +// client fields (`adapter`, `addr`, `flags`), and dereferencing to > +// `I2cClient` or `AsRef>` inherits their `Sync` i= mpls. > +unsafe impl Sync for I2cClient {} > + > /// The registration of an i2c client device. > /// > /// This type represents the registration of a [`struct i2c_client`]. Wh= en an instance of this > @@ -573,3 +586,231 @@ unsafe impl Send for Registration {} > // SAFETY: `Registration` offers no interior mutability (no mutation thr= ough &self > // and no mutable access is exposed) > unsafe impl Sync for Registration {} > + > +/// I/O backend for SMBus register access via I2C. > +/// > +/// This backend implements only [`FallibleIoCapable`] and not > +/// [`crate::io::IoCapable`], because I2C/SMBus transactions can fail > +/// regardless of address validity. The infallible [`crate::io::Io::read= `], > +/// [`crate::io::Io::write`], and [`crate::io::Io::update`] methods are > +/// therefore unavailable for this backend. > +pub struct I2cBackend; > + > +/// An SMBus view associated with a bound I2C client. > +/// > +/// The pointer encodes a command offset and is never dereferenced as > +/// memory. Its type and, for dynamically sized targets, metadata descri= be > +/// the extent of the view. > +/// > +/// # Invariants > +/// > +/// The view is derived from the client's SMBus command region. > +/// Projections preserve the bounds of the source view. > +/// `client` remains valid and bound for the lifetime of the view. > +pub struct I2cView<'a, T: ?Sized> { > + client: &'a I2cClient, > + ptr: *mut T, > +} > + > +impl Copy for I2cView<'_, T> {} > + > +impl Clone for I2cView<'_, T> { > + #[inline] > + fn clone(&self) -> Self { > + *self > + } > +} > + > +impl IoBackend for I2cBackend { > + type View<'a, T: ?Sized + KnownSize> =3D I2cView<'a, T>; > + > + #[inline] > + fn as_ptr<'a, T: ?Sized + KnownSize>(view: Self::View<'a, T>) -> *mu= t T { > + view.ptr > + } > + > + #[inline] > + unsafe fn project_view<'a, T: ?Sized + KnownSize, U: ?Sized + KnownS= ize>( > + view: Self::View<'a, T>, > + ptr: *mut U, > + ) -> Self::View<'a, U> { > + // INVARIANT: Per safety requirement. > + I2cView { > + client: view.client, > + ptr, > + } > + } > +} > + > +impl FallibleIoCapable for I2cBackend { > + #[inline] > + fn io_try_read<'a>(view: I2cView<'a, u8>) -> Result { > + // By `I2cView`'s invariant, `addr() + 1 <=3D 256`, so `addr()` = is at most > + // 255 and the `as u8` cast is lossless. > + let reg =3D Self::as_ptr(view).addr() as u8; > + view.client.smbus_read_byte(reg) > + } > + > + #[inline] > + fn io_try_write<'a>(view: I2cView<'a, u8>, value: u8) -> Result { > + // By `I2cView`'s invariant, `addr() + 1 <=3D 256`, so `addr()` = is at most > + // 255 and the `as u8` cast is lossless. > + let reg =3D Self::as_ptr(view).addr() as u8; > + view.client.smbus_write_byte(reg, value) > + } > +} > + > +/// The `Region<256>` view is byte-addressed. Consequently, its generic > +/// 16-bit operations reject odd command bytes and command byte `0xFF`. > +/// Devices whose command bytes each select an independent 16-bit regist= er > +/// should use [`I2cClient::smbus_read_word`] or > +/// [`I2cClient::smbus_read_word_swapped`] directly. > +impl FallibleIoCapable for I2cBackend { > + #[inline] > + fn io_try_read<'a>(view: I2cView<'a, u16>) -> Result { > + // By `I2cView`'s invariant, `addr() + 2 <=3D 256`, so `addr()` = is at most > + // 254 and the `as u8` cast is lossless. > + let reg =3D Self::as_ptr(view).addr() as u8; > + view.client.smbus_read_word(reg) > + } > + > + #[inline] > + fn io_try_write<'a>(view: I2cView<'a, u16>, value: u16) -> Result { > + // By `I2cView`'s invariant, `addr() + 2 <=3D 256`, so `addr()` = is at most > + // 254 and the `as u8` cast is lossless. > + let reg =3D Self::as_ptr(view).addr() as u8; > + view.client.smbus_write_word(reg, value) > + } > +} > + > +impl<'a, T: ?Sized + KnownSize> IoBase<'a> for I2cView<'a, T> { > + type Backend =3D I2cBackend; > + type Target =3D T; > + > + #[inline] > + fn as_view(self) -> I2cView<'a, T> { > + self > + } > +} > + > +// SAFETY: The bound client borrow remains valid when the view is moved > +// between threads. The view exposes only SMBus transactions, which use > +// the I2C core's bus locking, and pointer projections that never > +// dereference the encoded address as memory. `T: Sync` preserves the > +// shared-view restriction. > +unsafe impl Send for I2cView<'_, T> {} > + > +// SAFETY: Sharing a view permits concurrent SMBus transactions, which > +// use the I2C core's bus locking. The bound client remains valid for > +// the view's lifetime, and the encoded pointer is never dereferenced > +// as memory. `T: Sync` preserves the shared-view restriction. > +unsafe impl Sync for I2cView<'_, T> {} > + > +impl I2cClient { > + /// Returns an I/O handle for SMBus register access on this I2C clie= nt. > + /// > + /// The returned handle provides fallible read and write methods for= the > + /// SMBus command byte range (`0x00..=3D0xFF`). A command byte is a = data byte which > + /// often selects a register on the device. The 7-bit device address= is handled > + /// separately by the I2C core. > + /// > + /// [`crate::io::Io::try_read16`] and > + /// [`crate::io::Io::try_write16`] on the returned handle reject > + /// odd offsets because the [`Region`]-based bounds check requires > + /// `offset % 2 =3D=3D 0`. This model does not cover every SMBus reg= ister layout, > + /// including layouts where auto-increment or auto-decrement results= in command > + /// bytes that appear unaligned. Use [`Self::smbus_read_word`] or > + /// [`Self::smbus_read_word_swapped`] for word access at an odd comm= and byte. > + #[inline] > + pub fn smbus_io(&self) -> I2cView<'_, Region<256>> { > + // INVARIANT: The view borrows this bound client and covers the = 256-byte > + // SMBus command region. > + // > + // The provenance-free pointer encodes a command offset rather t= han a > + // memory address. Its initial address is zero, and the region m= etadata > + // supplies the length used for bounds checks. Projections selec= t command > + // offsets; SMBus accessors extract those offsets without derefe= rencing > + // the pointer. > + I2cView { > + client: self, > + ptr: Region::<256>::ptr_from_raw_parts_mut(core::ptr::withou= t_provenance_mut(0), 256), > + } > + } > + > + #[inline] > + fn smbus_read_byte(&self, reg: u8) -> Result { > + // SAFETY: `self.as_raw()` returns a valid `*mut struct i2c_clie= nt` > + // pointer as guaranteed by the type invariant of `I2cClient`. > + // `i2c_smbus_read_byte_data` accepts any u8 command byte. > + let ret =3D unsafe { bindings::i2c_smbus_read_byte_data(self.as_= raw(), reg) }; > + to_result(ret)?; > + Ok(ret as u8) > + } > + > + #[inline] > + fn smbus_write_byte(&self, reg: u8, value: u8) -> Result { > + // SAFETY: `self.as_raw()` returns a valid `*mut struct i2c_clie= nt` > + // pointer as guaranteed by the type invariant of `I2cClient`. > + // `i2c_smbus_write_byte_data` accepts any u8 command byte and v= alue. > + let ret =3D unsafe { bindings::i2c_smbus_write_byte_data(self.as= _raw(), reg, value) }; > + to_result(ret) > + } > + > + /// Reads the 16-bit SMBus word value returned for a command. > + /// > + /// Wraps `i2c_smbus_read_word_data`. The `reg` parameter is the SMB= us > + /// command byte (`0x00..=3D0xFF`), a data byte which often selects = a register on > + /// the device. SMBus transmits the low byte first. > + /// > + /// Use [`Self::smbus_read_word_swapped`] for devices that transmit = the > + /// high byte of a multi-byte value first. > + /// > + /// Returns `Err` if the bus transaction fails, for example because = of a > + /// NACK, arbitration loss, or timeout. > + #[inline] > + pub fn smbus_read_word(&self, reg: u8) -> Result { > + // SAFETY: `self.as_raw()` returns a valid `*mut struct i2c_clie= nt` > + // pointer as guaranteed by the type invariant of `I2cClient`. > + // `i2c_smbus_read_word_data` is safe to call with any valid cli= ent > + // pointer and any u8 command byte. > + let ret =3D unsafe { bindings::i2c_smbus_read_word_data(self.as_= raw(), reg) }; > + to_result(ret)?; > + Ok(ret as u16) > + } > + > + #[inline] > + fn smbus_write_word(&self, reg: u8, value: u16) -> Result { > + // SAFETY: `self.as_raw()` returns a valid `*mut struct i2c_clie= nt` > + // pointer as guaranteed by the type invariant of `I2cClient`. > + // `i2c_smbus_write_word_data` accepts any u8 command byte and u= 16 value. > + let ret =3D unsafe { bindings::i2c_smbus_write_word_data(self.as= _raw(), reg, value) }; > + to_result(ret) > + } > + > + /// Reads a 16-bit word from an SMBus command and swaps its two byte= s. > + /// > + /// Wraps `i2c_smbus_read_word_data` and applies [`u16::swap_bytes`]= to the > + /// result. The `reg` parameter is the SMBus command byte (`0x00..= =3D0xFF`), a data > + /// byte which often selects a register on the device. > + /// > + /// SMBus transmits the low byte first. This method swaps the return= ed bytes > + /// for devices that transmit the high byte of a multi-byte value fi= rst. > + /// > + /// Returns `Err` if the bus transaction fails, for example because = of a > + /// NACK, arbitration loss, or timeout. > + /// > + /// # Examples > + /// > + /// ```no_run > + /// use kernel::{device::Bound, i2c::I2cClient, prelude::*}; > + /// > + /// # fn test(client: &I2cClient) -> Result { > + /// // Read a word from a device that transmits its high byte first. > + /// client.smbus_read_word_swapped(0x0c) > + /// # } > + /// ``` > + #[inline] > + pub fn smbus_read_word_swapped(&self, reg: u8) -> Result { > + self.smbus_read_word(reg).map(u16::swap_bytes) > + } > +} Tested-by: Markus Probst Thanks - Markus Probst --=-LFzGMxqsNpbYQl5YFmIm Content-Type: application/pgp-signature; name="signature.asc" Content-Description: This is a digitally signed message part -----BEGIN PGP SIGNATURE----- iQJPBAABCAA5FiEEgnQYxPSsWOdyMMRzNHYf+OetQ9IFAmsPBscbFIAAAAAABAAO bWFudTIsMi41KzEuMTIsMiwyAAoJEDR2H/jnrUPSdi4P/1gGSTmkWeO5tVYmMaw8 Snt8kTXEu/dCFQ/mtUi86t+lxqRcnozSZon+XU+OvdwpS907T5+BZ8YiydSYlCgb OQ8mtvd/kRPkDeXNFMhbaDkv8wH9SSL/uM10Dub3Kkf1KfXkg/M6ikoTv85VsooM TIQyOhd05M/sUZ0n9VxZgTR4zwAO7FWZK9iEKqGC9cbKIOMnVRW07h0sHPVnmw/Q gTPpNQeUp0r1/AyQiYTzjMKxSvYZ7d87WTWDaJ7Lz5r34K7D7oZudA6YnwZmy1+G +SRw9Dvof9c0DJd38QYtWIJFDDEKHESAjcZ8C2RZU0cH+yVlKgi1sYYxxJwRM6DB mWgcIBIvjZwMgaEW0HyL6zp5fcTpC2mmi6c4qpla2I1uP1rtraNp/fjwxAf90qoj U2sCuJMdVFScbNoZWrDKN2tDfoc5ZScL7MRQ9v5OJ/d7+MPXh8ZUaQS0XMPRSRIc sVophCBnF/u7gy10QqTzLQmDNviWLAEqHz0gpJiy/9DfZc2BZkBSNh1FJtm03uY+ Av/aomMtoDhPqK7b3vIcBS3lfws3MxvfjsYthDmFvShNW/FL5UGMFDCX+C5eDDdb 1KevTVZ0QOPa7H5lQ11LbkHFVWRQ92+1ra48kEnN5PHmNsTRqeroW1P4HD7mnmZD vGpqmKwcUE2dAKe3ld1VnKIO =KAr/ -----END PGP SIGNATURE----- --=-LFzGMxqsNpbYQl5YFmIm--