twizzler_driver/device/
mod.rs1use std::fmt::Display;
4
5use twizzler::object::{ObjID, Object, RawObject};
6pub use twizzler_abi::device::{BusType, DeviceRepr, DeviceType};
7use twizzler_abi::kso::{KactionCmd, KactionFlags, KactionGenericCmd, KactionValue};
8
9mod children;
10pub mod events;
11mod info;
12mod mmio;
13
14pub use children::DeviceChildrenIterator;
15pub use info::InfoObject;
16pub use mmio::MmioObject;
17use twizzler_rt_abi::{object::MapFlags, Result};
18
19pub struct Device {
21 obj: Object<DeviceRepr>,
22}
23
24impl Display for Device {
25 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26 let repr = self.repr();
27 repr.fmt(f)
28 }
29}
30
31impl Device {
32 pub fn new(id: ObjID) -> Result<Self> {
33 let obj = unsafe { Object::map_unchecked(id, MapFlags::READ | MapFlags::WRITE) }?;
34
35 Ok(Self { obj })
36 }
37
38 fn get_subobj(&self, ty: u8, idx: u8) -> Option<ObjID> {
39 let cmd = KactionCmd::Generic(KactionGenericCmd::GetSubObject(ty, idx));
40 let result = twizzler_abi::syscall::sys_kaction(
41 cmd,
42 Some(self.obj.id()),
43 0,
44 0,
45 KactionFlags::empty(),
46 )
47 .ok()?;
48 result.objid()
49 }
50
51 pub fn repr(&self) -> &DeviceRepr {
53 unsafe { self.obj.base_ptr::<DeviceRepr>().as_ref().unwrap() }
54 }
55
56 pub fn repr_mut(&self) -> &mut DeviceRepr {
58 unsafe { self.obj.base_mut_ptr::<DeviceRepr>().as_mut().unwrap() }
59 }
60
61 pub fn is_bus(&self) -> bool {
63 let repr = self.repr();
64 repr.device_type == DeviceType::Bus
65 }
66
67 pub fn bus_type(&self) -> BusType {
69 self.repr().bus_type
70 }
71
72 pub fn kaction(
74 &self,
75 action: KactionCmd,
76 value: u64,
77 flags: KactionFlags,
78 value2: u64,
79 ) -> Result<KactionValue> {
80 twizzler_abi::syscall::sys_kaction(action, Some(self.obj.id()), value, value2, flags)
81 }
82
83 pub fn id(&self) -> ObjID {
84 self.obj.id()
85 }
86}