xref: /linux/samples/rust/rust_driver_auxiliary.rs (revision 6f7e6393d1ce636bb7ec77a7fe7b77458fddf701)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 //! Rust auxiliary driver sample (based on a PCI driver for QEMU's `pci-testdev`).
4 //!
5 //! To make this driver probe, QEMU must be run with `-device pci-testdev`.
6 
7 use kernel::{
8     auxiliary, c_str,
9     device::{Bound, Core},
10     devres::Devres,
11     driver,
12     error::Error,
13     pci,
14     prelude::*,
15     InPlaceModule,
16 };
17 
18 use core::any::TypeId;
19 
20 const MODULE_NAME: &CStr = <LocalModule as kernel::ModuleMetadata>::NAME;
21 const AUXILIARY_NAME: &CStr = c_str!("auxiliary");
22 
23 struct AuxiliaryDriver;
24 
25 kernel::auxiliary_device_table!(
26     AUX_TABLE,
27     MODULE_AUX_TABLE,
28     <AuxiliaryDriver as auxiliary::Driver>::IdInfo,
29     [(auxiliary::DeviceId::new(MODULE_NAME, AUXILIARY_NAME), ())]
30 );
31 
32 impl auxiliary::Driver for AuxiliaryDriver {
33     type IdInfo = ();
34 
35     const ID_TABLE: auxiliary::IdTable<Self::IdInfo> = &AUX_TABLE;
36 
37     fn probe(adev: &auxiliary::Device<Core>, _info: &Self::IdInfo) -> impl PinInit<Self, Error> {
38         dev_info!(
39             adev.as_ref(),
40             "Probing auxiliary driver for auxiliary device with id={}\n",
41             adev.id()
42         );
43 
44         ParentDriver::connect(adev)?;
45 
46         Ok(Self)
47     }
48 }
49 
50 #[pin_data]
51 struct ParentDriver {
52     private: TypeId,
53     #[pin]
54     _reg0: Devres<auxiliary::Registration>,
55     #[pin]
56     _reg1: Devres<auxiliary::Registration>,
57 }
58 
59 kernel::pci_device_table!(
60     PCI_TABLE,
61     MODULE_PCI_TABLE,
62     <ParentDriver as pci::Driver>::IdInfo,
63     [(pci::DeviceId::from_id(pci::Vendor::REDHAT, 0x5), ())]
64 );
65 
66 impl pci::Driver for ParentDriver {
67     type IdInfo = ();
68 
69     const ID_TABLE: pci::IdTable<Self::IdInfo> = &PCI_TABLE;
70 
71     fn probe(pdev: &pci::Device<Core>, _info: &Self::IdInfo) -> impl PinInit<Self, Error> {
72         try_pin_init!(Self {
73             private: TypeId::of::<Self>(),
74             _reg0 <- auxiliary::Registration::new(pdev.as_ref(), AUXILIARY_NAME, 0, MODULE_NAME),
75             _reg1 <- auxiliary::Registration::new(pdev.as_ref(), AUXILIARY_NAME, 1, MODULE_NAME),
76         })
77     }
78 }
79 
80 impl ParentDriver {
81     fn connect(adev: &auxiliary::Device<Bound>) -> Result {
82         let dev = adev.parent();
83         let pdev: &pci::Device<Bound> = dev.try_into()?;
84         let drvdata = dev.drvdata::<Self>()?;
85 
86         dev_info!(
87             dev,
88             "Connect auxiliary {} with parent: VendorID={}, DeviceID={:#x}\n",
89             adev.id(),
90             pdev.vendor_id(),
91             pdev.device_id()
92         );
93 
94         dev_info!(
95             dev,
96             "We have access to the private data of {:?}.\n",
97             drvdata.private
98         );
99 
100         Ok(())
101     }
102 }
103 
104 #[pin_data]
105 struct SampleModule {
106     #[pin]
107     _pci_driver: driver::Registration<pci::Adapter<ParentDriver>>,
108     #[pin]
109     _aux_driver: driver::Registration<auxiliary::Adapter<AuxiliaryDriver>>,
110 }
111 
112 impl InPlaceModule for SampleModule {
113     fn init(module: &'static kernel::ThisModule) -> impl PinInit<Self, Error> {
114         try_pin_init!(Self {
115             _pci_driver <- driver::Registration::new(MODULE_NAME, module),
116             _aux_driver <- driver::Registration::new(MODULE_NAME, module),
117         })
118     }
119 }
120 
121 module! {
122     type: SampleModule,
123     name: "rust_driver_auxiliary",
124     authors: ["Danilo Krummrich"],
125     description: "Rust auxiliary driver",
126     license: "GPL v2",
127 }
128