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