|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | + |
| 3 | +//! Rust LDD scull |
| 4 | +//! reference: https://github.com/d0u9/Linux-Device-Driver/tree/master/eg_03_scull_basic |
| 5 | +//! |
| 6 | +//! How to build only modules: |
| 7 | +//! make LLVM=1 M=samples/rust |
| 8 | +use core::marker::PhantomPinned; |
| 9 | +use kernel::prelude::*; |
| 10 | +use kernel::{ |
| 11 | + bindings, |
| 12 | + file::{self, File}, |
| 13 | + fmt, |
| 14 | + io_buffer::{IoBufferReader, IoBufferWriter}, |
| 15 | + miscdev, pin_init, |
| 16 | + sync::{Arc, ArcBorrow}, |
| 17 | + types::Opaque, |
| 18 | +}; |
| 19 | + |
| 20 | +module! { |
| 21 | + type: RustCompletion, |
| 22 | + name: "rust_completion", |
| 23 | + author: "Rust for Linux Contributors", |
| 24 | + description: "Rust LDD ch06 scull", |
| 25 | + license: "GPL", |
| 26 | +} |
| 27 | + |
| 28 | +// internal info between file operations |
| 29 | +#[pin_data] |
| 30 | +struct CompletionDev { |
| 31 | + pub completion: bindings::completion, |
| 32 | + |
| 33 | + #[pin] |
| 34 | + _pin: PhantomPinned, |
| 35 | +} |
| 36 | + |
| 37 | +// TODO: impl CompletionDev::try_new |
| 38 | +impl CompletionDev { |
| 39 | + fn try_new() -> Result<Arc<Self>> { |
| 40 | + pr_info!("completion_dev created\n"); |
| 41 | + |
| 42 | + Ok(Arc::try_new(Self { |
| 43 | + _pin: PhantomPinned, |
| 44 | + completion: bindings::completion::default(), |
| 45 | + })?) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +unsafe impl Sync for CompletionDev {} |
| 50 | +unsafe impl Send for CompletionDev {} |
| 51 | + |
| 52 | +// unit struct for file operations |
| 53 | +struct RustFile; |
| 54 | + |
| 55 | +#[vtable] |
| 56 | +impl file::Operations for RustFile { |
| 57 | + type Data = Arc<CompletionDev>; |
| 58 | + type OpenData = Arc<CompletionDev>; |
| 59 | + |
| 60 | + fn open(shared: &Arc<CompletionDev>, _file: &file::File) -> Result<Self::Data> { |
| 61 | + pr_info!("open is invoked\n",); |
| 62 | + Ok(shared.clone()) |
| 63 | + } |
| 64 | + |
| 65 | + // |
| 66 | + fn read( |
| 67 | + shared: ArcBorrow<'_, CompletionDev>, |
| 68 | + _: &File, |
| 69 | + data: &mut impl IoBufferWriter, |
| 70 | + offset: u64, |
| 71 | + ) -> Result<usize> { |
| 72 | + pr_info!("read is invoked\n"); |
| 73 | + |
| 74 | + Ok(0) |
| 75 | + } |
| 76 | + |
| 77 | + fn write( |
| 78 | + shared: ArcBorrow<'_, CompletionDev>, |
| 79 | + _: &File, |
| 80 | + data: &mut impl IoBufferReader, |
| 81 | + offset: u64, |
| 82 | + ) -> Result<usize> { |
| 83 | + pr_debug!("write is invoked\n"); |
| 84 | + |
| 85 | + let len: usize = data.len(); |
| 86 | + pr_info!("write: {} bytes\n", len); |
| 87 | + Ok(len) |
| 88 | + } |
| 89 | + |
| 90 | + fn release(_data: Self::Data, _file: &File) { |
| 91 | + pr_info!("release is invoked\n"); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +struct RustCompletion { |
| 96 | + _dev: Pin<Box<miscdev::Registration<RustFile>>>, |
| 97 | +} |
| 98 | + |
| 99 | +impl kernel::Module for RustCompletion { |
| 100 | + fn init(_module: &'static ThisModule) -> Result<Self> { |
| 101 | + pr_info!("rust_ldd06 is loaded\n"); |
| 102 | + |
| 103 | + let dev: Arc<CompletionDev> = CompletionDev::try_new()?; |
| 104 | + let reg = miscdev::Registration::new_pinned(fmt!("rust_ldd06"), dev)?; |
| 105 | + |
| 106 | + Ok(RustCompletion { _dev: reg }) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +impl Drop for RustCompletion { |
| 111 | + fn drop(&mut self) { |
| 112 | + pr_info!("rust_ldd06 is unloaded\n"); |
| 113 | + } |
| 114 | +} |
0 commit comments