lib.rs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // SPDX-License-Identifier: GPL-2.0
  2. //! Crate for all kernel procedural macros.
  3. mod helpers;
  4. mod module;
  5. use proc_macro::TokenStream;
  6. /// Declares a kernel module.
  7. ///
  8. /// The `type` argument should be a type which implements the [`Module`]
  9. /// trait. Also accepts various forms of kernel metadata.
  10. ///
  11. /// C header: [`include/linux/moduleparam.h`](../../../include/linux/moduleparam.h)
  12. ///
  13. /// [`Module`]: ../kernel/trait.Module.html
  14. ///
  15. /// # Examples
  16. ///
  17. /// ```ignore
  18. /// use kernel::prelude::*;
  19. ///
  20. /// module!{
  21. /// type: MyModule,
  22. /// name: b"my_kernel_module",
  23. /// author: b"Rust for Linux Contributors",
  24. /// description: b"My very own kernel module!",
  25. /// license: b"GPL",
  26. /// params: {
  27. /// my_i32: i32 {
  28. /// default: 42,
  29. /// permissions: 0o000,
  30. /// description: b"Example of i32",
  31. /// },
  32. /// writeable_i32: i32 {
  33. /// default: 42,
  34. /// permissions: 0o644,
  35. /// description: b"Example of i32",
  36. /// },
  37. /// },
  38. /// }
  39. ///
  40. /// struct MyModule;
  41. ///
  42. /// impl kernel::Module for MyModule {
  43. /// fn init() -> Result<Self> {
  44. /// // If the parameter is writeable, then the kparam lock must be
  45. /// // taken to read the parameter:
  46. /// {
  47. /// let lock = THIS_MODULE.kernel_param_lock();
  48. /// pr_info!("i32 param is: {}\n", writeable_i32.read(&lock));
  49. /// }
  50. /// // If the parameter is read only, it can be read without locking
  51. /// // the kernel parameters:
  52. /// pr_info!("i32 param is: {}\n", my_i32.read());
  53. /// Ok(Self)
  54. /// }
  55. /// }
  56. /// ```
  57. ///
  58. /// # Supported argument types
  59. /// - `type`: type which implements the [`Module`] trait (required).
  60. /// - `name`: byte array of the name of the kernel module (required).
  61. /// - `author`: byte array of the author of the kernel module.
  62. /// - `description`: byte array of the description of the kernel module.
  63. /// - `license`: byte array of the license of the kernel module (required).
  64. /// - `alias`: byte array of alias name of the kernel module.
  65. #[proc_macro]
  66. pub fn module(ts: TokenStream) -> TokenStream {
  67. module::module(ts)
  68. }