History

rust: driver: decouple driver private data from driver type

Add a type Data<'bound> associated type to all bus driver traits, decoupling the driver's bus device private data type from the driver struct itself. In the context of adding a 'bound lifetime, making this an associated type has the advantage that it allows us to avoid a driver trait global lifetime and it avoids the need for ForLt for bus device private data; both of which make the subsequent implementation by buses much simpler. All existing drivers and doc examples set type Data = Self to preserve the current behavior.

Danilo Krummrich committed 5 months ago

Commit 7fdffdd1 parent c8a436622 files · +98 −55Browse files

Unified · Split

22 files
  1. drivers/cpufreq/rcpufreq_dt.rs +1 −0
  2. drivers/gpu/drm/nova/driver.rs +1 −0
  3. drivers/gpu/drm/tyr/driver.rs +1 −0
  4. drivers/gpu/nova-core/driver.rs +1 −0
  5. drivers/pwm/pwm_th1520.rs +1 −0
  6. rust/kernel/auxiliary.rs +11 −7
  7. rust/kernel/cpufreq.rs +1 −0
  8. rust/kernel/driver.rs +14 −10
  9. rust/kernel/i2c.rs +18 −14
  10. rust/kernel/io/mem.rs +2 −0
  11. rust/kernel/pci.rs +13 −8
  12. rust/kernel/platform.rs +12 −8
  13. rust/kernel/usb.rs +12 −8
  14. samples/rust/rust_debugfs.rs +1 −0
  15. samples/rust/rust_dma.rs +1 −0
  16. samples/rust/rust_driver_auxiliary.rs +2 −0
  17. samples/rust/rust_driver_i2c.rs +1 −0
  18. samples/rust/rust_driver_pci.rs +1 −0
  19. samples/rust/rust_driver_platform.rs +1 −0
  20. samples/rust/rust_driver_usb.rs +1 −0
  21. samples/rust/rust_i2c_client.rs +1 −0
  22. samples/rust/rust_soc.rs +1 −0
drivers/cpufreq/rcpufreq_dt.rs+1−0
@@ -201,6 +201,7 @@ kernel::of_device_table!( · 200 unmodified lines
201201
202202 impl platform::Driver for CPUFreqDTDriver {
203203 type IdInfo = ();
204+ type Data = Self;
204205 const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);
205206
206207 fn probe(
drivers/gpu/drm/nova/driver.rs+1−0
@@ -51,6 +51,7 @@ kernel::auxiliary_device_table!( · 50 unmodified lines
5151
5252 impl auxiliary::Driver for NovaDriver {
5353 type IdInfo = ();
54+ type Data = Self;
5455 const ID_TABLE: auxiliary::IdTable<Self::IdInfo> = &AUX_TABLE;
5556
5657 fn probe(adev: &auxiliary::Device<Core>, _info: &Self::IdInfo) -> impl PinInit<Self, Error> {
drivers/gpu/drm/tyr/driver.rs+1−0
@@ -91,6 +91,7 @@ kernel::of_device_table!( · 90 unmodified lines
9191
9292 impl platform::Driver for TyrPlatformDriverData {
9393 type IdInfo = ();
94+ type Data = Self;
9495 const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);
9596
9697 fn probe(
drivers/gpu/nova-core/driver.rs+1−0
@@ -74,6 +74,7 @@ kernel::pci_device_table!( · 73 unmodified lines
7474
7575 impl pci::Driver for NovaCore {
7676 type IdInfo = ();
77+ type Data = Self;
7778 const ID_TABLE: pci::IdTable<Self::IdInfo> = &PCI_TABLE;
7879
7980 fn probe(pdev: &pci::Device<Core>, _info: &Self::IdInfo) -> impl PinInit<Self, Error> {
drivers/pwm/pwm_th1520.rs+1−0
@@ -316,6 +316,7 @@ kernel::of_device_table!( · 315 unmodified lines
316316
317317 impl platform::Driver for Th1520PwmPlatformDriver {
318318 type IdInfo = ();
319+ type Data = Self;
319320 const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);
320321
321322 fn probe(
rust/kernel/auxiliary.rs+11−7
@@ -41,12 +41,12 @@ pub struct Adapter<T: Driver>(T); · 40 unmodified lines
4141
4242 // SAFETY:
4343 // - `bindings::auxiliary_driver` is a C type declared as `repr(C)`.
44−// - `T` is the type of the driver's device private data.
44+// - `T::Data` is the type of the driver's device private data.
4545 // - `struct auxiliary_driver` embeds a `struct device_driver`.
4646 // - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
4747 unsafe impl<T: Driver> driver::DriverLayout for Adapter<T> {
4848 type DriverType = bindings::auxiliary_driver;
49− type DriverData = T;
49+ type DriverData<'bound> = T::Data;
5050 const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
5151 }
5252
@@ -111,8 +111,8 @@ impl<T: Driver> Adapter<T> { · 58 unmodified lines
111111
112112 // SAFETY: `remove_callback` is only ever called after a successful call to
113113 // `probe_callback`, hence it's guaranteed that `Device::set_drvdata()` has been called
114− // and stored a `Pin<KBox<T>>`.
115− let data = unsafe { adev.as_ref().drvdata_borrow::<T>() };
114+ // and stored a `Pin<KBox<T::Data>>`.
115+ let data = unsafe { adev.as_ref().drvdata_borrow::<T::Data>() };
116116
117117 T::unbind(adev, data);
118118 }
@@ -202,13 +202,17 @@ pub trait Driver { · 83 unmodified lines
202202 /// type IdInfo: 'static = ();
203203 type IdInfo: 'static;
204204
205+ /// The type of the driver's bus device private data.
206+ type Data: Send;
207+
205208 /// The table of device ids supported by the driver.
206209 const ID_TABLE: IdTable<Self::IdInfo>;
207210
208211 /// Auxiliary driver probe.
209212 ///
210213 /// Called when an auxiliary device is matches a corresponding driver.
211− fn probe(dev: &Device<device::Core>, id_info: &Self::IdInfo) -> impl PinInit<Self, Error>;
214+ fn probe(dev: &Device<device::Core>, id_info: &Self::IdInfo)
215+ -> impl PinInit<Self::Data, Error>;
212216
213217 /// Auxiliary driver unbind.
214218 ///
@@ -219,8 +223,8 @@ pub trait Driver { · 4 unmodified lines
219223 /// `&Device<Core>` or `&Device<Bound>` reference. For instance, drivers may try to perform I/O
220224 /// operations to gracefully tear down the device.
221225 ///
222− /// Otherwise, release operations for driver resources should be performed in `Self::drop`.
223− fn unbind(dev: &Device<device::Core>, this: Pin<&Self>) {
226+ /// Otherwise, release operations for driver resources should be performed in `Drop`.
227+ fn unbind(dev: &Device<device::Core>, this: Pin<&Self::Data>) {
224228 let _ = (dev, this);
225229 }
226230 }
rust/kernel/cpufreq.rs+1−0
@@ -888,6 +888,7 @@ pub trait Driver { · 887 unmodified lines
888888 ///
889889 /// impl platform::Driver for SampleDriver {
890890 /// type IdInfo = ();
891+/// type Data = Self;
891892 /// const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = None;
892893 ///
893894 /// fn probe(
rust/kernel/driver.rs+14−10
@@ -13,10 +13,13 @@ · 12 unmodified lines
1313 //! The main driver interface is defined by a bus specific driver trait. For instance:
1414 //!
1515 //! ```ignore
16−//! pub trait Driver: Send {
16+//! pub trait Driver {
1717 //! /// The type holding information about each device ID supported by the driver.
1818 //! type IdInfo: 'static;
1919 //!
20+//! /// The type of the driver's bus device private data.
21+//! type Data: Send;
22+//!
2023 //! /// The table of OF device ids supported by the driver.
2124 //! const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = None;
2225 //!
@@ -24,10 +27,11 @@ · 1 unmodified line
2427 //! const ACPI_ID_TABLE: Option<acpi::IdTable<Self::IdInfo>> = None;
2528 //!
2629 //! /// Driver probe.
27−//! fn probe(dev: &Device<device::Core>, id_info: &Self::IdInfo) -> impl PinInit<Self, Error>;
30+//! fn probe(dev: &Device<device::Core>, id_info: &Self::IdInfo)
31+//! -> impl PinInit<Self::Data, Error>;
2832 //!
2933 //! /// Driver unbind (optional).
30−//! fn unbind(dev: &Device<device::Core>, this: Pin<&Self>) {
34+//! fn unbind(dev: &Device<device::Core>, this: Pin<&Self::Data>) {
3135 //! let _ = (dev, this);
3236 //! }
3337 //! }
@@ -42,9 +46,9 @@ · 8 unmodified lines
4246 )]
4347 #![cfg_attr(CONFIG_PCI, doc = "* [`pci::Driver`](kernel::pci::Driver)")]
4448 //!
45−//! The `probe()` callback should return a `impl PinInit<Self, Error>`, i.e. the driver's private
46−//! data. The bus abstraction should store the pointer in the corresponding bus device. The generic
47−//! [`Device`] infrastructure provides common helpers for this purpose on its
49+//! The `probe()` callback should return a `impl PinInit<Self::Data, Error>`, i.e. the driver's
50+//! private data. The bus abstraction should store the pointer in the corresponding bus device. The
51+//! generic [`Device`] infrastructure provides common helpers for this purpose on its
4852 //! [`Device<CoreInternal>`] implementation.
4953 //!
5054 //! All driver callbacks should provide a reference to the driver's private data. Once the driver
@@ -118,8 +122,8 @@ pub unsafe trait DriverLayout { · 67 unmodified lines
118122 /// The specific driver type embedding a `struct device_driver`.
119123 type DriverType: Default;
120124
121− /// The type of the driver's device private data.
122− type DriverData;
125+ /// The type of the driver's bus device private data.
126+ type DriverData<'bound>;
123127
124128 /// Byte offset of the embedded `struct device_driver` within `DriverType`.
125129 ///
@@ -193,8 +197,8 @@ impl<T: RegistrationOps> Registration<T> { · 67 unmodified lines
193197 // driver's device private data.
194198 //
195199 // SAFETY: By the safety requirements of the `Driver` trait, `T::DriverData` is the
196− // driver's device private data type.
197− drop(unsafe { dev.drvdata_obtain::<T::DriverData>() });
200+ // driver's bus device private data type.
201+ drop(unsafe { dev.drvdata_obtain::<T::DriverData<'_>>() });
198202 }
199203
200204 /// Attach generic `struct device_driver` callbacks.
rust/kernel/i2c.rs+18−14
@@ -93,12 +93,12 @@ pub struct Adapter<T: Driver>(T); · 92 unmodified lines
9393
9494 // SAFETY:
9595 // - `bindings::i2c_driver` is a C type declared as `repr(C)`.
96−// - `T` is the type of the driver's device private data.
96+// - `T::Data` is the type of the driver's device private data.
9797 // - `struct i2c_driver` embeds a `struct device_driver`.
9898 // - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
9999 unsafe impl<T: Driver> driver::DriverLayout for Adapter<T> {
100100 type DriverType = bindings::i2c_driver;
101− type DriverData = T;
101+ type DriverData<'bound> = T::Data;
102102 const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
103103 }
104104
@@ -176,8 +176,8 @@ impl<T: Driver> Adapter<T> { · 71 unmodified lines
176176
177177 // SAFETY: `remove_callback` is only ever called after a successful call to
178178 // `probe_callback`, hence it's guaranteed that `I2cClient::set_drvdata()` has been called
179− // and stored a `Pin<KBox<T>>`.
180− let data = unsafe { idev.as_ref().drvdata_borrow::<T>() };
179+ // and stored a `Pin<KBox<T::Data>>`.
180+ let data = unsafe { idev.as_ref().drvdata_borrow::<T::Data>() };
181181
182182 T::unbind(idev, data);
183183 }
@@ -188,8 +188,8 @@ impl<T: Driver> Adapter<T> { · 4 unmodified lines
188188
189189 // SAFETY: `shutdown_callback` is only ever called after a successful call to
190190 // `probe_callback`, hence it's guaranteed that `Device::set_drvdata()` has been called
191− // and stored a `Pin<KBox<T>>`.
192− let data = unsafe { idev.as_ref().drvdata_borrow::<T>() };
191+ // and stored a `Pin<KBox<T::Data>>`.
192+ let data = unsafe { idev.as_ref().drvdata_borrow::<T::Data>() };
193193
194194 T::shutdown(idev, data);
195195 }
@@ -294,6 +294,7 @@ macro_rules! module_i2c_driver { · 98 unmodified lines
294294 ///
295295 /// impl i2c::Driver for MyDriver {
296296 /// type IdInfo = ();
297+/// type Data = Self;
297298 /// const I2C_ID_TABLE: Option<i2c::IdTable<Self::IdInfo>> = Some(&I2C_TABLE);
298299 /// const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);
299300 /// const ACPI_ID_TABLE: Option<acpi::IdTable<Self::IdInfo>> = Some(&ACPI_TABLE);
@@ -301,15 +302,15 @@ macro_rules! module_i2c_driver { · 1 unmodified line
301302 /// fn probe(
302303 /// _idev: &i2c::I2cClient<Core>,
303304 /// _id_info: Option<&Self::IdInfo>,
304−/// ) -> impl PinInit<Self, Error> {
305+/// ) -> impl PinInit<Self::Data, Error> {
305306 /// Err(ENODEV)
306307 /// }
307308 ///
308−/// fn shutdown(_idev: &i2c::I2cClient<Core>, this: Pin<&Self>) {
309+/// fn shutdown(_idev: &i2c::I2cClient<Core>, this: Pin<&Self::Data>) {
309310 /// }
310311 /// }
311312 ///```
312−pub trait Driver: Send {
313+pub trait Driver {
313314 /// The type holding information about each device id supported by the driver.
314315 // TODO: Use `associated_type_defaults` once stabilized:
315316 //
@@ -318,6 +319,9 @@ pub trait Driver: Send { · 2 unmodified lines
318319 // ```
319320 type IdInfo: 'static;
320321
322+ /// The type of the driver's bus device private data.
323+ type Data: Send;
324+
321325 /// The table of device ids supported by the driver.
322326 const I2C_ID_TABLE: Option<IdTable<Self::IdInfo>> = None;
323327
@@ -334,7 +338,7 @@ pub trait Driver: Send { · 10 unmodified lines
334338 fn probe(
335339 dev: &I2cClient<device::Core>,
336340 id_info: Option<&Self::IdInfo>,
337− ) -> impl PinInit<Self, Error>;
341+ ) -> impl PinInit<Self::Data, Error>;
338342
339343 /// I2C driver shutdown.
340344 ///
@@ -346,8 +350,8 @@ pub trait Driver: Send { · 5 unmodified lines
346350 ///
347351 /// This callback is distinct from final resource cleanup, as the driver instance remains valid
348352 /// after it returns. Any deallocation or teardown of driver-owned resources should instead be
349− /// handled in `Self::drop`.
350− fn shutdown(dev: &I2cClient<device::Core>, this: Pin<&Self>) {
353+ /// handled in `Drop`.
354+ fn shutdown(dev: &I2cClient<device::Core>, this: Pin<&Self::Data>) {
351355 let _ = (dev, this);
352356 }
353357
@@ -360,8 +364,8 @@ pub trait Driver: Send { · 6 unmodified lines
360364 /// `&Device<Core>` or `&Device<Bound>` reference. For instance, drivers may try to perform I/O
361365 /// operations to gracefully tear down the device.
362366 ///
363− /// Otherwise, release operations for driver resources should be performed in `Self::drop`.
364− fn unbind(dev: &I2cClient<device::Core>, this: Pin<&Self>) {
367+ /// Otherwise, release operations for driver resources should be performed in `Drop`.
368+ fn unbind(dev: &I2cClient<device::Core>, this: Pin<&Self::Data>) {
365369 let _ = (dev, this);
366370 }
367371 }
rust/kernel/io/mem.rs+2−0
@@ -62,6 +62,7 @@ impl<'a> IoRequest<'a> { · 61 unmodified lines
6262 ///
6363 /// impl platform::Driver for SampleDriver {
6464 /// # type IdInfo = ();
65+ /// # type Data = Self;
6566 ///
6667 /// fn probe(
6768 /// pdev: &platform::Device<Core>,
@@ -126,6 +127,7 @@ impl<'a> IoRequest<'a> { · 58 unmodified lines
126127 ///
127128 /// impl platform::Driver for SampleDriver {
128129 /// # type IdInfo = ();
130+ /// # type Data = Self;
129131 ///
130132 /// fn probe(
131133 /// pdev: &platform::Device<Core>,
rust/kernel/pci.rs+13−8
@@ -59,12 +59,12 @@ pub struct Adapter<T: Driver>(T); · 58 unmodified lines
5959
6060 // SAFETY:
6161 // - `bindings::pci_driver` is a C type declared as `repr(C)`.
62−// - `T` is the type of the driver's device private data.
62+// - `T::Data` is the type of the driver's device private data.
6363 // - `struct pci_driver` embeds a `struct device_driver`.
6464 // - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
6565 unsafe impl<T: Driver> driver::DriverLayout for Adapter<T> {
6666 type DriverType = bindings::pci_driver;
67− type DriverData = T;
67+ type DriverData<'bound> = T::Data;
6868 const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
6969 }
7070
@@ -129,8 +129,8 @@ impl<T: Driver> Adapter<T> { · 58 unmodified lines
129129
130130 // SAFETY: `remove_callback` is only ever called after a successful call to
131131 // `probe_callback`, hence it's guaranteed that `Device::set_drvdata()` has been called
132− // and stored a `Pin<KBox<T>>`.
133− let data = unsafe { pdev.as_ref().drvdata_borrow::<T>() };
132+ // and stored a `Pin<KBox<T::Data>>`.
133+ let data = unsafe { pdev.as_ref().drvdata_borrow::<T::Data>() };
134134
135135 T::unbind(pdev, data);
136136 }
@@ -279,6 +279,7 @@ macro_rules! pci_device_table { · 142 unmodified lines
279279 ///
280280 /// impl pci::Driver for MyDriver {
281281 /// type IdInfo = ();
282+/// type Data = Self;
282283 /// const ID_TABLE: pci::IdTable<Self::IdInfo> = &PCI_TABLE;
283284 ///
284285 /// fn probe(
@@ -291,7 +292,7 @@ macro_rules! pci_device_table { · 6 unmodified lines
291292 ///```
292293 /// Drivers must implement this trait in order to get a PCI driver registered. Please refer to the
293294 /// `Adapter` documentation for an example.
294−pub trait Driver: Send {
295+pub trait Driver {
295296 /// The type holding information about each device id supported by the driver.
296297 // TODO: Use `associated_type_defaults` once stabilized:
297298 //
@@ -300,6 +301,9 @@ pub trait Driver: Send { · 2 unmodified lines
300301 // ```
301302 type IdInfo: 'static;
302303
304+ /// The type of the driver's bus device private data.
305+ type Data: Send;
306+
303307 /// The table of device ids supported by the driver.
304308 const ID_TABLE: IdTable<Self::IdInfo>;
305309
@@ -307,7 +311,8 @@ pub trait Driver: Send { · 1 unmodified line
307311 ///
308312 /// Called when a new pci device is added or discovered. Implementers should
309313 /// attempt to initialize the device here.
310− fn probe(dev: &Device<device::Core>, id_info: &Self::IdInfo) -> impl PinInit<Self, Error>;
314+ fn probe(dev: &Device<device::Core>, id_info: &Self::IdInfo)
315+ -> impl PinInit<Self::Data, Error>;
311316
312317 /// PCI driver unbind.
313318 ///
@@ -318,8 +323,8 @@ pub trait Driver: Send { · 4 unmodified lines
318323 /// `&Device<Core>` or `&Device<Bound>` reference. For instance, drivers may try to perform I/O
319324 /// operations to gracefully tear down the device.
320325 ///
321− /// Otherwise, release operations for driver resources should be performed in `Self::drop`.
322− fn unbind(dev: &Device<device::Core>, this: Pin<&Self>) {
326+ /// Otherwise, release operations for driver resources should be performed in `Drop`.
327+ fn unbind(dev: &Device<device::Core>, this: Pin<&Self::Data>) {
323328 let _ = (dev, this);
324329 }
325330 }
rust/kernel/platform.rs+12−8
@@ -45,12 +45,12 @@ pub struct Adapter<T: Driver>(T); · 44 unmodified lines
4545
4646 // SAFETY:
4747 // - `bindings::platform_driver` is a C type declared as `repr(C)`.
48−// - `T` is the type of the driver's device private data.
48+// - `T::Data` is the type of the driver's device private data.
4949 // - `struct platform_driver` embeds a `struct device_driver`.
5050 // - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
5151 unsafe impl<T: Driver> driver::DriverLayout for Adapter<T> {
5252 type DriverType = bindings::platform_driver;
53− type DriverData = T;
53+ type DriverData<'bound> = T::Data;
5454 const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
5555 }
5656
@@ -117,8 +117,8 @@ impl<T: Driver> Adapter<T> { · 60 unmodified lines
117117
118118 // SAFETY: `remove_callback` is only ever called after a successful call to
119119 // `probe_callback`, hence it's guaranteed that `Device::set_drvdata()` has been called
120− // and stored a `Pin<KBox<T>>`.
121− let data = unsafe { pdev.as_ref().drvdata_borrow::<T>() };
120+ // and stored a `Pin<KBox<T::Data>>`.
121+ let data = unsafe { pdev.as_ref().drvdata_borrow::<T::Data>() };
122122
123123 T::unbind(pdev, data);
124124 }
@@ -192,6 +192,7 @@ macro_rules! module_platform_driver { · 67 unmodified lines
192192 ///
193193 /// impl platform::Driver for MyDriver {
194194 /// type IdInfo = ();
195+/// type Data = Self;
195196 /// const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);
196197 /// const ACPI_ID_TABLE: Option<acpi::IdTable<Self::IdInfo>> = Some(&ACPI_TABLE);
197198 ///
@@ -203,7 +204,7 @@ macro_rules! module_platform_driver { · 5 unmodified lines
203204 /// }
204205 /// }
205206 ///```
206−pub trait Driver: Send {
207+pub trait Driver {
207208 /// The type holding driver private data about each device id supported by the driver.
208209 // TODO: Use associated_type_defaults once stabilized:
209210 //
@@ -212,6 +213,9 @@ pub trait Driver: Send { · 2 unmodified lines
212213 // ```
213214 type IdInfo: 'static;
214215
216+ /// The type of the driver's bus device private data.
217+ type Data: Send;
218+
215219 /// The table of OF device ids supported by the driver.
216220 const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = None;
217221
@@ -225,7 +229,7 @@ pub trait Driver: Send { · 7 unmodified lines
225229 fn probe(
226230 dev: &Device<device::Core>,
227231 id_info: Option<&Self::IdInfo>,
228− ) -> impl PinInit<Self, Error>;
232+ ) -> impl PinInit<Self::Data, Error>;
229233
230234 /// Platform driver unbind.
231235 ///
@@ -236,8 +240,8 @@ pub trait Driver: Send { · 4 unmodified lines
236240 /// `&Device<Core>` or `&Device<Bound>` reference. For instance, drivers may try to perform I/O
237241 /// operations to gracefully tear down the device.
238242 ///
239− /// Otherwise, release operations for driver resources should be performed in `Self::drop`.
240− fn unbind(dev: &Device<device::Core>, this: Pin<&Self>) {
243+ /// Otherwise, release operations for driver resources should be performed in `Drop`.
244+ fn unbind(dev: &Device<device::Core>, this: Pin<&Self::Data>) {
241245 let _ = (dev, this);
242246 }
243247 }
rust/kernel/usb.rs+12−8
@@ -36,12 +36,12 @@ pub struct Adapter<T: Driver>(T); · 35 unmodified lines
3636
3737 // SAFETY:
3838 // - `bindings::usb_driver` is a C type declared as `repr(C)`.
39−// - `T` is the type of the driver's device private data.
39+// - `T::Data` is the type of the driver's device private data.
4040 // - `struct usb_driver` embeds a `struct device_driver`.
4141 // - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
4242 unsafe impl<T: Driver> driver::DriverLayout for Adapter<T> {
4343 type DriverType = bindings::usb_driver;
44− type DriverData = T;
44+ type DriverData<'bound> = T::Data;
4545 const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
4646 }
4747
@@ -109,8 +109,8 @@ impl<T: Driver> Adapter<T> { · 61 unmodified lines
109109
110110 // SAFETY: `disconnect_callback` is only ever called after a successful call to
111111 // `probe_callback`, hence it's guaranteed that `Device::set_drvdata()` has been called
112− // and stored a `Pin<KBox<T>>`.
113− let data = unsafe { dev.drvdata_borrow::<T>() };
112+ // and stored a `Pin<KBox<T::Data>>`.
113+ let data = unsafe { dev.drvdata_borrow::<T::Data>() };
114114
115115 T::disconnect(intf, data);
116116 }
@@ -287,23 +287,27 @@ macro_rules! usb_device_table { · 170 unmodified lines
287287 ///
288288 /// impl usb::Driver for MyDriver {
289289 /// type IdInfo = ();
290+/// type Data = Self;
290291 /// const ID_TABLE: usb::IdTable<Self::IdInfo> = &USB_TABLE;
291292 ///
292293 /// fn probe(
293294 /// _interface: &usb::Interface<Core>,
294295 /// _id: &usb::DeviceId,
295296 /// _info: &Self::IdInfo,
296−/// ) -> impl PinInit<Self, Error> {
297+/// ) -> impl PinInit<Self::Data, Error> {
297298 /// Err(ENODEV)
298299 /// }
299300 ///
300−/// fn disconnect(_interface: &usb::Interface<Core>, _data: Pin<&Self>) {}
301+/// fn disconnect(_interface: &usb::Interface<Core>, _data: Pin<&Self::Data>) {}
301302 /// }
302303 ///```
303304 pub trait Driver {
304305 /// The type holding information about each one of the device ids supported by the driver.
305306 type IdInfo: 'static;
306307
308+ /// The type of the driver's bus device private data.
309+ type Data: Send;
310+
307311 /// The table of device ids supported by the driver.
308312 const ID_TABLE: IdTable<Self::IdInfo>;
309313
@@ -315,12 +319,12 @@ pub trait Driver { · 5 unmodified lines
315319 interface: &Interface<device::Core>,
316320 id: &DeviceId,
317321 id_info: &Self::IdInfo,
318− ) -> impl PinInit<Self, Error>;
322+ ) -> impl PinInit<Self::Data, Error>;
319323
320324 /// USB driver disconnect.
321325 ///
322326 /// Called when the USB interface is about to be unbound from this driver.
323− fn disconnect(interface: &Interface<device::Core>, data: Pin<&Self>);
327+ fn disconnect(interface: &Interface<device::Core>, data: Pin<&Self::Data>);
324328 }
325329
326330 /// A USB interface.
samples/rust/rust_debugfs.rs+1−0
@@ -117,6 +117,7 @@ kernel::acpi_device_table!( · 116 unmodified lines
117117
118118 impl platform::Driver for RustDebugFs {
119119 type IdInfo = ();
120+ type Data = Self;
120121 const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = None;
121122 const ACPI_ID_TABLE: Option<acpi::IdTable<Self::IdInfo>> = Some(&ACPI_TABLE);
122123
samples/rust/rust_dma.rs+1−0
@@ -58,6 +58,7 @@ kernel::pci_device_table!( · 57 unmodified lines
5858
5959 impl pci::Driver for DmaSampleDriver {
6060 type IdInfo = ();
61+ type Data = Self;
6162 const ID_TABLE: pci::IdTable<Self::IdInfo> = &PCI_TABLE;
6263
6364 fn probe(pdev: &pci::Device<Core>, _info: &Self::IdInfo) -> impl PinInit<Self, Error> {
samples/rust/rust_driver_auxiliary.rs+2−0
@@ -31,6 +31,7 @@ kernel::auxiliary_device_table!( · 30 unmodified lines
3131
3232 impl auxiliary::Driver for AuxiliaryDriver {
3333 type IdInfo = ();
34+ type Data = Self;
3435
3536 const ID_TABLE: auxiliary::IdTable<Self::IdInfo> = &AUX_TABLE;
3637
@@ -65,6 +66,7 @@ kernel::pci_device_table!( · 28 unmodified lines
6566
6667 impl pci::Driver for ParentDriver {
6768 type IdInfo = ();
69+ type Data = Self;
6870
6971 const ID_TABLE: pci::IdTable<Self::IdInfo> = &PCI_TABLE;
7072
samples/rust/rust_driver_i2c.rs+1−0
@@ -35,6 +35,7 @@ kernel::of_device_table! { · 34 unmodified lines
3535
3636 impl i2c::Driver for SampleDriver {
3737 type IdInfo = u32;
38+ type Data = Self;
3839
3940 const ACPI_ID_TABLE: Option<acpi::IdTable<Self::IdInfo>> = Some(&ACPI_TABLE);
4041 const I2C_ID_TABLE: Option<i2c::IdTable<Self::IdInfo>> = Some(&I2C_TABLE);
samples/rust/rust_driver_pci.rs+1−0
@@ -140,6 +140,7 @@ impl SampleDriver { · 139 unmodified lines
140140
141141 impl pci::Driver for SampleDriver {
142142 type IdInfo = TestIndex;
143+ type Data = Self;
143144
144145 const ID_TABLE: pci::IdTable<Self::IdInfo> = &PCI_TABLE;
145146
samples/rust/rust_driver_platform.rs+1−0
@@ -101,6 +101,7 @@ kernel::acpi_device_table!( · 100 unmodified lines
101101
102102 impl platform::Driver for SampleDriver {
103103 type IdInfo = Info;
104+ type Data = Self;
104105 const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);
105106 const ACPI_ID_TABLE: Option<acpi::IdTable<Self::IdInfo>> = Some(&ACPI_TABLE);
106107
samples/rust/rust_driver_usb.rs+1−0
@@ -26,6 +26,7 @@ kernel::usb_device_table!( · 25 unmodified lines
2626
2727 impl usb::Driver for SampleDriver {
2828 type IdInfo = ();
29+ type Data = Self;
2930 const ID_TABLE: usb::IdTable<Self::IdInfo> = &USB_TABLE;
3031
3132 fn probe(
samples/rust/rust_i2c_client.rs+1−0
@@ -106,6 +106,7 @@ const BOARD_INFO: i2c::I2cBoardInfo = · 105 unmodified lines
106106
107107 impl platform::Driver for SampleDriver {
108108 type IdInfo = ();
109+ type Data = Self;
109110 const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);
110111 const ACPI_ID_TABLE: Option<acpi::IdTable<Self::IdInfo>> = Some(&ACPI_TABLE);
111112
samples/rust/rust_soc.rs+1−0
@@ -37,6 +37,7 @@ kernel::acpi_device_table!( · 36 unmodified lines
3737
3838 impl platform::Driver for SampleSocDriver {
3939 type IdInfo = ();
40+ type Data = Self;
4041 const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);
4142 const ACPI_ID_TABLE: Option<acpi::IdTable<Self::IdInfo>> = Some(&ACPI_TABLE);
4243