summaryrefslogtreecommitdiff
path: root/samples
diff options
context:
space:
mode:
Diffstat (limited to 'samples')
-rw-r--r--samples/rust/rust_misc_device.rs29
1 files changed, 19 insertions, 10 deletions
diff --git a/samples/rust/rust_misc_device.rs b/samples/rust/rust_misc_device.rs
index 41e26c825060..f5b2b00b933a 100644
--- a/samples/rust/rust_misc_device.rs
+++ b/samples/rust/rust_misc_device.rs
@@ -156,7 +156,11 @@ impl kernel::InPlaceModule for RustMiscDeviceModule {
};
try_pin_init!(Self {
- _miscdev <- MiscDeviceRegistration::register(options),
+ // SAFETY: `_miscdev` is stored in `RustMiscDeviceModule` and dropped when the module
+ // is unloaded; `mem::forget()` is never called on it.
+ _miscdev <- unsafe {
+ MiscDeviceRegistration::register(options, try_pin_init!(RustMiscDevice {}))
+ },
})
}
}
@@ -166,8 +170,13 @@ struct Inner {
buffer: KVVec<u8>,
}
+/// Data shared by all open files for the same instance of this miscdevice.
+#[pin_data]
+struct RustMiscDevice {}
+
+/// Data that is stored per file for this miscdevice.
#[pin_data(PinnedDrop)]
-struct RustMiscDevice {
+struct RustMiscDeviceFile {
#[pin]
inner: Mutex<Inner>,
dev: ARef<Device>,
@@ -175,16 +184,16 @@ struct RustMiscDevice {
#[vtable]
impl MiscDevice for RustMiscDevice {
- type Ptr = Pin<KBox<Self>>;
+ type File = Pin<KBox<RustMiscDeviceFile>>;
- fn open(_file: &File, misc: &MiscDeviceRegistration<Self>) -> Result<Pin<KBox<Self>>> {
+ fn open(_file: &File, misc: &MiscDeviceRegistration<Self>) -> Result<Self::File> {
let dev = ARef::from(misc.device());
dev_info!(dev, "Opening Rust Misc Device Sample\n");
KBox::try_pin_init(
try_pin_init! {
- RustMiscDevice {
+ RustMiscDeviceFile {
inner <- new_mutex!(Inner {
value: 0_i32,
buffer: KVVec::new(),
@@ -196,7 +205,7 @@ impl MiscDevice for RustMiscDevice {
)
}
- fn read_iter(mut kiocb: Kiocb<'_, Self::Ptr>, iov: &mut IovIterDest<'_>) -> Result<usize> {
+ fn read_iter(mut kiocb: Kiocb<'_, Self::File>, iov: &mut IovIterDest<'_>) -> Result<usize> {
let me = kiocb.file();
dev_info!(me.dev, "Reading from Rust Misc Device Sample\n");
@@ -207,7 +216,7 @@ impl MiscDevice for RustMiscDevice {
Ok(read)
}
- fn write_iter(mut kiocb: Kiocb<'_, Self::Ptr>, iov: &mut IovIterSource<'_>) -> Result<usize> {
+ fn write_iter(mut kiocb: Kiocb<'_, Self::File>, iov: &mut IovIterSource<'_>) -> Result<usize> {
let me = kiocb.file();
dev_info!(me.dev, "Writing to Rust Misc Device Sample\n");
@@ -223,7 +232,7 @@ impl MiscDevice for RustMiscDevice {
Ok(len)
}
- fn ioctl(me: Pin<&RustMiscDevice>, _file: &File, cmd: u32, arg: usize) -> Result<isize> {
+ fn ioctl(me: Pin<&RustMiscDeviceFile>, _file: &File, cmd: u32, arg: usize) -> Result<isize> {
dev_info!(me.dev, "IOCTLing Rust Misc Device Sample\n");
// Treat the ioctl argument as a user pointer.
@@ -245,13 +254,13 @@ impl MiscDevice for RustMiscDevice {
}
#[pinned_drop]
-impl PinnedDrop for RustMiscDevice {
+impl PinnedDrop for RustMiscDeviceFile {
fn drop(self: Pin<&mut Self>) {
dev_info!(self.dev, "Exiting the Rust Misc Device Sample\n");
}
}
-impl RustMiscDevice {
+impl RustMiscDeviceFile {
fn set_value(&self, mut reader: UserSliceReader) -> Result<isize> {
let new_value = reader.read::<i32>()?;
let mut guard = self.inner.lock();