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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
use std::{
collections::HashMap,
path::{Path, PathBuf},
sync::Arc,
};
use phf::{phf_map, phf_set};
use crate::{
types::*,
dbus_helpers::props::*,
};
static I2C_HWMON_DEVICES: phf::Map<&'static str, bool> = phf_map! {
"DPS310" => false,
"EMC1412" => true,
"EMC1413" => true,
"EMC1414" => true,
"HDC1080" => false,
"JC42" => true,
"LM75A" => true,
"LM95234" => true,
"MAX31725" => true,
"MAX31730" => true,
"MAX6581" => true,
"MAX6654" => true,
"NCT6779" => true,
"NCT7802" => true,
"SBTSI" => true,
"SI7020" => false,
"TMP112" => true,
"TMP175" => true,
"TMP421" => true,
"TMP441" => true,
"TMP75" => true,
"W83773G" => true,
};
static I2C_PMBUS_TYPES: phf::Set<&'static str> = phf_set! {
"ADM1266",
"ADM1272",
"ADM1275",
"ADM1278",
"ADM1293",
"ADS7830",
"BMR490",
"DPS800",
"INA219",
"INA230",
"IPSPS",
"IR38060",
"IR38164",
"IR38263",
"ISL68137",
"ISL68220",
"ISL68223",
"ISL69225",
"ISL69243",
"ISL69260",
"LM25066",
"MAX16601",
"MAX20710",
"MAX20730",
"MAX20734",
"MAX20796",
"MAX34451",
"MP2971",
"MP2973",
"MP5023",
"PLI1209BC",
"pmbus",
"PXE1610",
"RAA228000",
"RAA228228",
"RAA228620",
"RAA229001",
"RAA229004",
"RAA229126",
"TPS53679",
"TPS546D24",
"XDPE11280",
"XDPE12284"
};
pub fn get_device_type(s: &str) -> Option<I2CDeviceType> {
if let Some(k) = I2C_PMBUS_TYPES.get_key(s) {
Some(I2CDeviceType::PMBus(PMBusDeviceType { name: k }))
} else if let Some((k, v)) = I2C_HWMON_DEVICES.get_entry(s) {
Some(I2CDeviceType::Hwmon(HwmonDeviceType {
name: k,
creates_hwmon: *v,
}))
} else {
None
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct HwmonDeviceType {
name: &'static str,
creates_hwmon: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct PMBusDeviceType {
name: &'static str,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum I2CDeviceType {
PMBus(PMBusDeviceType),
Hwmon(HwmonDeviceType),
}
impl I2CDeviceType {
pub fn name(&self) -> &'static str {
match self {
Self::PMBus(p) => p.name,
Self::Hwmon(h) => h.name,
}
}
pub fn creates_hwmon(&self) -> bool {
match self {
Self::PMBus(_) => true,
Self::Hwmon(h) => h.creates_hwmon,
}
}
fn kernel_type(&self) -> String {
self.name().to_lowercase()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct I2CDeviceParams {
pub bus: u16,
pub address: u16,
pub devtype: I2CDeviceType,
}
const I2C_DEV_DIR: &str = "/sys/bus/i2c/devices";
impl I2CDeviceParams {
pub fn from_dbus(cfg: &dbus::arg::PropMap, devtype: I2CDeviceType) -> ErrResult<Self> {
let bus: u64 = *prop_get_mandatory(cfg, "Bus")?;
let address: u64 = *prop_get_mandatory(cfg, "Address")?;
Ok(Self {
bus: bus.try_into()?,
address: address.try_into()?,
devtype,
})
}
pub fn sysfs_name(&self) -> String {
format!("{}-{:04x}", self.bus, self.address)
}
pub fn sysfs_device_dir(&self) -> PathBuf {
Path::new(I2C_DEV_DIR).join(self.sysfs_name())
}
pub fn sysfs_bus_dir(&self) -> PathBuf {
Path::new(I2C_DEV_DIR).join(format!("i2c-{}", self.bus))
}
pub fn device_present(&self) -> bool {
let mut path = self.sysfs_device_dir();
if self.devtype.creates_hwmon() {
path.push("hwmon");
}
path.exists()
}
pub fn device_static(&self) -> bool {
if !self.device_present() {
false
} else {
self.sysfs_device_dir().join("of_node").exists()
}
}
pub fn instantiate_device(&self) -> ErrResult<Option<Arc<I2CDevice>>> {
if self.device_static() {
Ok(None)
} else {
if self.device_present() {
drop(I2CDevice::new(self.clone()));
}
I2CDevice::new(self.clone()).map(|d| Some(Arc::new(d)))
}
}
}
pub struct I2CDevice {
pub params: I2CDeviceParams,
}
impl I2CDevice {
pub fn new(params: I2CDeviceParams) -> ErrResult<Self> {
let dev = Self { params };
if dev.params.device_present() {
return Ok(dev);
}
let ctor_path = dev.params.sysfs_bus_dir().join("new_device");
let payload = format!("{} {:#02x}\n", dev.params.devtype.kernel_type(),
dev.params.address);
std::fs::write(&ctor_path, payload)?;
if dev.params.device_present() {
Ok(dev)
} else {
Err(err_other("new_device failed to instantiate device"))
}
}
}
impl Drop for I2CDevice {
fn drop(&mut self) {
let dtor_path = self.params.sysfs_bus_dir().join("delete_device");
let payload = format!("{:#02x}\n", self.params.address);
if let Err(e) = std::fs::write(&dtor_path, payload) {
eprintln!("Failed to write to {}: {}", dtor_path.display(), e);
}
}
}
pub type I2CDeviceMap = HashMap<I2CDeviceParams, std::sync::Weak<I2CDevice>>;
pub fn get_i2cdev(devmap: &mut I2CDeviceMap, params: &I2CDeviceParams)
-> ErrResult<Option<Arc<I2CDevice>>>
{
let d = devmap.get(params).and_then(|w| w.upgrade());
if d.is_some() {
Ok(d)
} else {
let dev = params.instantiate_device()?;
if let Some(ref d) = dev {
devmap.insert(params.clone(), Arc::downgrade(d));
}
Ok(dev)
}
}