@@ -641,7 +641,7 @@ pub fn module(ts: TokenStream) -> TokenStream {
641
641
}}
642
642
643
643
fn __init() -> kernel::c_types::c_int {{
644
- match <{type_} as KernelModule>::init() {{
644
+ match <{type_} as kernel:: KernelModule>::init() {{
645
645
Ok(m) => {{
646
646
unsafe {{
647
647
__MOD = Some(m);
@@ -683,3 +683,81 @@ pub fn module(ts: TokenStream) -> TokenStream {
683
683
initcall_section = ".initcall6.init"
684
684
) . parse ( ) . expect ( "Error parsing formatted string into token stream." )
685
685
}
686
+
687
+ /// Declares a kernel module that exposes a single misc device.
688
+ ///
689
+ /// The `type` argument should be a type which implements the [`FileOpener`] trait. Also accepts
690
+ /// various forms of kernel metadata.
691
+ ///
692
+ /// [`FileOpener`]: ../kernel/file_operations/trait.FileOpener.html
693
+ ///
694
+ /// # Examples
695
+ ///
696
+ /// ```rust,no_run
697
+ /// use kernel::prelude::*;
698
+ ///
699
+ /// module_misc_device! {
700
+ /// type: MyFile,
701
+ /// name: b"my_miscdev_kernel_module",
702
+ /// author: b"Rust for Linux Contributors",
703
+ /// description: b"My very own misc device kernel module!",
704
+ /// license: b"GPL v2",
705
+ /// }
706
+ ///
707
+ /// #[derive(Default)]
708
+ /// struct MyFile;
709
+ ///
710
+ /// impl kernel::file_operations::FileOperations for MyFile {
711
+ /// kernel::declare_file_operations!();
712
+ /// }
713
+ /// ```
714
+ #[ proc_macro]
715
+ pub fn module_misc_device ( ts : TokenStream ) -> TokenStream {
716
+ let mut it = ts. into_iter ( ) ;
717
+
718
+ let type_ = get_ident ( & mut it, "type" ) ;
719
+ let name = get_byte_string ( & mut it, "name" ) ;
720
+ let author = get_byte_string ( & mut it, "author" ) ;
721
+ let description = get_byte_string ( & mut it, "description" ) ;
722
+ let license = get_byte_string ( & mut it, "license" ) ;
723
+ expect_end ( & mut it) ;
724
+
725
+ let module = format ! ( "__internal_ModuleFor{}" , type_) ;
726
+
727
+ format ! (
728
+ "
729
+ struct {module} {{
730
+ _dev: core::pin::Pin<alloc::boxed::Box<kernel::miscdev::Registration>>,
731
+ }}
732
+
733
+ impl kernel::KernelModule for {module} {{
734
+ fn init() -> kernel::KernelResult<Self> {{
735
+ Ok(Self {{
736
+ _dev: kernel::miscdev::Registration::new_pinned::<{type_}>(
737
+ kernel::cstr!(\" {name}\" ),
738
+ None,
739
+ (),
740
+ )?,
741
+ }})
742
+ }}
743
+ }}
744
+
745
+ kernel::prelude::module! {{
746
+ type: {module},
747
+ name: b\" {name}\" ,
748
+ author: b\" {author}\" ,
749
+ description: b\" {description}\" ,
750
+ license: b\" {license}\" ,
751
+ params: {{}},
752
+ }}
753
+ " ,
754
+ module = module,
755
+ type_ = type_,
756
+ name = name,
757
+ author = author,
758
+ description = description,
759
+ license = license
760
+ )
761
+ . parse ( )
762
+ . expect ( "Error parsing formatted string into token stream." )
763
+ }
0 commit comments