1*a03c4f9dSLoGin use super::{ 2*a03c4f9dSLoGin bus::{bus_manager, Bus}, 3*a03c4f9dSLoGin Device, DeviceMatchName, DeviceMatcher, IdTable, 4*a03c4f9dSLoGin }; 5*a03c4f9dSLoGin use crate::{ 6*a03c4f9dSLoGin driver::base::kobject::KObject, 7*a03c4f9dSLoGin filesystem::sysfs::{sysfs_instance, Attribute, AttributeGroup}, 8*a03c4f9dSLoGin syscall::SystemError, 9*a03c4f9dSLoGin }; 10*a03c4f9dSLoGin use alloc::{sync::Arc, vec::Vec}; 110663027bSTingHuang use core::fmt::Debug; 122a7d773dSTingHuang 132a7d773dSTingHuang /// @brief: Driver error 142a7d773dSTingHuang #[allow(dead_code)] 1578bf93f0SYJwu2023 #[derive(Debug, PartialEq, Eq, Clone, Copy)] 162a7d773dSTingHuang pub enum DriverError { 17b087521eSChiichen ProbeError, // 探测设备失败(该驱动不能初始化这个设备) 18b087521eSChiichen RegisterError, // 设备注册失败 19b087521eSChiichen AllocateResourceError, // 获取设备所需资源失败 20b087521eSChiichen UnsupportedOperation, // 不支持的操作 21b087521eSChiichen UnInitialized, // 未初始化 222a7d773dSTingHuang } 232a7d773dSTingHuang 24e0de0fd6STingHuang impl Into<SystemError> for DriverError { 25e0de0fd6STingHuang fn into(self) -> SystemError { 26e0de0fd6STingHuang match self { 27b087521eSChiichen DriverError::ProbeError => SystemError::ENODEV, 28b087521eSChiichen DriverError::RegisterError => SystemError::ENODEV, 29b087521eSChiichen DriverError::AllocateResourceError => SystemError::EIO, 30b087521eSChiichen DriverError::UnsupportedOperation => SystemError::EIO, 31b087521eSChiichen DriverError::UnInitialized => SystemError::ENODEV, 32e0de0fd6STingHuang } 33e0de0fd6STingHuang } 34e0de0fd6STingHuang } 35e0de0fd6STingHuang 3606d5e247SLoGin #[inline(always)] 3706d5e247SLoGin pub fn driver_manager() -> &'static DriverManager { 3806d5e247SLoGin &DriverManager 39e0de0fd6STingHuang } 40e0de0fd6STingHuang 41*a03c4f9dSLoGin /// 驱动程序应当实现的trait 42*a03c4f9dSLoGin /// 43*a03c4f9dSLoGin /// ## 注意 44*a03c4f9dSLoGin /// 45*a03c4f9dSLoGin /// 由于设备驱动模型需要从Arc<dyn KObject>转换为Arc<dyn Driver>, 46*a03c4f9dSLoGin /// 因此,所有的实现了 Driver trait的结构体,都应该在结构体上方标注`#[cast_to([sync] Driver)]`, 47*a03c4f9dSLoGin /// 否则在运行时会报错 48*a03c4f9dSLoGin pub trait Driver: Sync + Send + Debug + KObject { 49*a03c4f9dSLoGin fn coredump(&self, _device: &Arc<dyn Device>) -> Result<(), SystemError> { 50*a03c4f9dSLoGin Err(SystemError::EOPNOTSUPP_OR_ENOTSUP) 51*a03c4f9dSLoGin } 52*a03c4f9dSLoGin 53*a03c4f9dSLoGin /// @brief: 获取驱动标识符 54*a03c4f9dSLoGin /// @parameter: None 55*a03c4f9dSLoGin /// @return: 该驱动驱动唯一标识符 56*a03c4f9dSLoGin fn id_table(&self) -> Option<IdTable>; 57*a03c4f9dSLoGin 58*a03c4f9dSLoGin fn devices(&self) -> Vec<Arc<dyn Device>>; 59*a03c4f9dSLoGin 60*a03c4f9dSLoGin /// 把设备加入当前驱动管理的列表中 61*a03c4f9dSLoGin fn add_device(&self, device: Arc<dyn Device>); 62*a03c4f9dSLoGin 63*a03c4f9dSLoGin /// 从当前驱动管理的列表中删除设备 64*a03c4f9dSLoGin fn delete_device(&self, device: &Arc<dyn Device>); 65*a03c4f9dSLoGin 66*a03c4f9dSLoGin /// 根据设备名称查找绑定到驱动的设备 67*a03c4f9dSLoGin /// 68*a03c4f9dSLoGin /// 该方法是一个快速查找方法,要求驱动开发者自行实现。 69*a03c4f9dSLoGin /// 70*a03c4f9dSLoGin /// 如果开发者没有实现该方法,则应当返回None 71*a03c4f9dSLoGin /// 72*a03c4f9dSLoGin /// ## 注意 73*a03c4f9dSLoGin /// 74*a03c4f9dSLoGin /// 这是一个内部方法,不应当被外部调用,若要查找设备,请使用`find_device_by_name()` 75*a03c4f9dSLoGin fn __find_device_by_name_fast(&self, _name: &str) -> Option<Arc<dyn Device>> { 76*a03c4f9dSLoGin None 77*a03c4f9dSLoGin } 78*a03c4f9dSLoGin 79*a03c4f9dSLoGin /// 是否禁用sysfs的bind/unbind属性 80*a03c4f9dSLoGin /// 81*a03c4f9dSLoGin /// ## 返回 82*a03c4f9dSLoGin /// 83*a03c4f9dSLoGin /// - true: 禁用 84*a03c4f9dSLoGin /// - false: 不禁用(默认) 85*a03c4f9dSLoGin fn suppress_bind_attrs(&self) -> bool { 86*a03c4f9dSLoGin false 87*a03c4f9dSLoGin } 88*a03c4f9dSLoGin 89*a03c4f9dSLoGin fn bus(&self) -> Option<Arc<dyn Bus>> { 90*a03c4f9dSLoGin None 91*a03c4f9dSLoGin } 92*a03c4f9dSLoGin 93*a03c4f9dSLoGin fn set_bus(&self, bus: Option<Arc<dyn Bus>>); 94*a03c4f9dSLoGin 95*a03c4f9dSLoGin fn groups(&self) -> &'static [&'static dyn AttributeGroup] { 96*a03c4f9dSLoGin &[] 97*a03c4f9dSLoGin } 98*a03c4f9dSLoGin 99*a03c4f9dSLoGin fn dev_groups(&self) -> &'static [&'static dyn AttributeGroup] { 100*a03c4f9dSLoGin &[] 101*a03c4f9dSLoGin } 102*a03c4f9dSLoGin 103*a03c4f9dSLoGin /// 使用什么样的策略来探测设备 104*a03c4f9dSLoGin fn probe_type(&self) -> DriverProbeType { 105*a03c4f9dSLoGin DriverProbeType::DefaultStrategy 106*a03c4f9dSLoGin } 107*a03c4f9dSLoGin } 108*a03c4f9dSLoGin 109*a03c4f9dSLoGin impl dyn Driver { 110*a03c4f9dSLoGin pub fn allows_async_probing(&self) -> bool { 111*a03c4f9dSLoGin match self.probe_type() { 112*a03c4f9dSLoGin DriverProbeType::PreferAsync => true, 113*a03c4f9dSLoGin DriverProbeType::ForceSync => false, 114*a03c4f9dSLoGin DriverProbeType::DefaultStrategy => { 115*a03c4f9dSLoGin // todo: 判断是否请求异步探测,如果是的话,就返回true 116*a03c4f9dSLoGin 117*a03c4f9dSLoGin // 由于目前还没有支持异步探测,因此这里暂时返回false 118*a03c4f9dSLoGin false 119*a03c4f9dSLoGin } 120*a03c4f9dSLoGin } 121*a03c4f9dSLoGin } 122*a03c4f9dSLoGin 123*a03c4f9dSLoGin /// 根据条件寻找一个绑定到这个驱动的设备(低效实现) 124*a03c4f9dSLoGin /// 125*a03c4f9dSLoGin /// ## 参数 126*a03c4f9dSLoGin /// 127*a03c4f9dSLoGin /// - `matcher` - 匹配器 128*a03c4f9dSLoGin /// - `data` - 传给匹配器的数据 129*a03c4f9dSLoGin /// 130*a03c4f9dSLoGin /// ## 注意 131*a03c4f9dSLoGin /// 132*a03c4f9dSLoGin /// 这里的默认实现很低效,请为特定的驱动自行实现高效的查询 133*a03c4f9dSLoGin fn find_device_slow<T: Copy>( 134*a03c4f9dSLoGin &self, 135*a03c4f9dSLoGin matcher: &dyn DeviceMatcher<T>, 136*a03c4f9dSLoGin data: T, 137*a03c4f9dSLoGin ) -> Option<Arc<dyn Device>> { 138*a03c4f9dSLoGin for dev in self.devices() { 139*a03c4f9dSLoGin if matcher.match_device(&dev, data) { 140*a03c4f9dSLoGin return Some(dev); 141*a03c4f9dSLoGin } 142*a03c4f9dSLoGin } 143*a03c4f9dSLoGin 144*a03c4f9dSLoGin return None; 145*a03c4f9dSLoGin } 146*a03c4f9dSLoGin 147*a03c4f9dSLoGin /// 根据设备名称查找绑定到驱动的设备 148*a03c4f9dSLoGin /// 149*a03c4f9dSLoGin /// ## 注意 150*a03c4f9dSLoGin /// 151*a03c4f9dSLoGin /// 这里的默认实现很低效,请为特定的驱动自行实现高效的查询 152*a03c4f9dSLoGin pub fn find_device_by_name(&self, name: &str) -> Option<Arc<dyn Device>> { 153*a03c4f9dSLoGin if let Some(r) = self.__find_device_by_name_fast(name) { 154*a03c4f9dSLoGin return Some(r); 155*a03c4f9dSLoGin } 156*a03c4f9dSLoGin 157*a03c4f9dSLoGin return self.find_device_slow(&DeviceMatchName, name); 158*a03c4f9dSLoGin } 159*a03c4f9dSLoGin } 160*a03c4f9dSLoGin 161e0de0fd6STingHuang /// @brief: 驱动管理器 162e0de0fd6STingHuang #[derive(Debug, Clone)] 16306d5e247SLoGin pub struct DriverManager; 164e0de0fd6STingHuang 165e0de0fd6STingHuang impl DriverManager { 166*a03c4f9dSLoGin /// 注册设备驱动。该设备驱动应当已经设置好其bus字段 167*a03c4f9dSLoGin /// 168*a03c4f9dSLoGin /// ## 参数 169*a03c4f9dSLoGin /// 170*a03c4f9dSLoGin /// - driver: 驱动 171*a03c4f9dSLoGin /// 172*a03c4f9dSLoGin /// 参考 https://opengrok.ringotek.cn/xref/linux-6.1.9/drivers/base/driver.c#222 173*a03c4f9dSLoGin pub fn register(&self, driver: Arc<dyn Driver>) -> Result<(), SystemError> { 174*a03c4f9dSLoGin let bus = driver.bus().ok_or_else(|| { 175*a03c4f9dSLoGin kerror!( 176*a03c4f9dSLoGin "DriverManager::register() failed: driver.bus() is None. Driver: '{:?}'", 177*a03c4f9dSLoGin driver.name() 178*a03c4f9dSLoGin ); 179*a03c4f9dSLoGin SystemError::EINVAL 180*a03c4f9dSLoGin })?; 181*a03c4f9dSLoGin 182*a03c4f9dSLoGin let drv_name = driver.name(); 183*a03c4f9dSLoGin let other = bus.find_driver_by_name(&drv_name); 184*a03c4f9dSLoGin if other.is_some() { 185*a03c4f9dSLoGin kerror!( 186*a03c4f9dSLoGin "DriverManager::register() failed: driver '{}' already registered", 187*a03c4f9dSLoGin drv_name 188*a03c4f9dSLoGin ); 189*a03c4f9dSLoGin return Err(SystemError::EBUSY); 190*a03c4f9dSLoGin } 191*a03c4f9dSLoGin 192*a03c4f9dSLoGin bus_manager().add_driver(&driver)?; 193*a03c4f9dSLoGin 194*a03c4f9dSLoGin self.add_groups(&driver, driver.groups()).map_err(|e| { 195*a03c4f9dSLoGin bus_manager().remove_driver(&driver); 196*a03c4f9dSLoGin e 197*a03c4f9dSLoGin })?; 198*a03c4f9dSLoGin 199*a03c4f9dSLoGin // todo: 发送uevent 200*a03c4f9dSLoGin 201*a03c4f9dSLoGin return Ok(()); 202*a03c4f9dSLoGin } 203*a03c4f9dSLoGin 204*a03c4f9dSLoGin /// 从系统中删除一个驱动程序 205*a03c4f9dSLoGin #[allow(dead_code)] 206*a03c4f9dSLoGin pub fn unregister(&self, driver: &Arc<dyn Driver>) { 207*a03c4f9dSLoGin self.remove_groups(driver, driver.groups()); 208*a03c4f9dSLoGin bus_manager().remove_driver(driver); 209*a03c4f9dSLoGin } 210*a03c4f9dSLoGin 21106d5e247SLoGin /// 参考: https://opengrok.ringotek.cn/xref/linux-6.1.9/drivers/base/dd.c#434 21206d5e247SLoGin pub fn driver_sysfs_add(&self, _dev: &Arc<dyn Device>) -> Result<(), SystemError> { 21306d5e247SLoGin todo!("DriverManager::driver_sysfs_add()"); 214e0de0fd6STingHuang } 215*a03c4f9dSLoGin 216*a03c4f9dSLoGin pub fn add_groups( 217*a03c4f9dSLoGin &self, 218*a03c4f9dSLoGin driver: &Arc<dyn Driver>, 219*a03c4f9dSLoGin groups: &'static [&dyn AttributeGroup], 220*a03c4f9dSLoGin ) -> Result<(), SystemError> { 221*a03c4f9dSLoGin let kobj = driver.clone() as Arc<dyn KObject>; 222*a03c4f9dSLoGin return sysfs_instance().create_groups(&kobj, groups); 223*a03c4f9dSLoGin } 224*a03c4f9dSLoGin 225*a03c4f9dSLoGin pub fn remove_groups(&self, driver: &Arc<dyn Driver>, groups: &'static [&dyn AttributeGroup]) { 226*a03c4f9dSLoGin let kobj = driver.clone() as Arc<dyn KObject>; 227*a03c4f9dSLoGin sysfs_instance().remove_groups(&kobj, groups); 228*a03c4f9dSLoGin } 229*a03c4f9dSLoGin 230*a03c4f9dSLoGin /// 为指定的驱动创建一个属性文件 231*a03c4f9dSLoGin /// 232*a03c4f9dSLoGin /// ## 参数 233*a03c4f9dSLoGin /// 234*a03c4f9dSLoGin /// - `driver` 要创建属性文件的驱动 235*a03c4f9dSLoGin /// - `attr` 属性 236*a03c4f9dSLoGin pub fn create_attr_file( 237*a03c4f9dSLoGin &self, 238*a03c4f9dSLoGin driver: &Arc<dyn Driver>, 239*a03c4f9dSLoGin attr: &'static dyn Attribute, 240*a03c4f9dSLoGin ) -> Result<(), SystemError> { 241*a03c4f9dSLoGin let kobj = driver.clone() as Arc<dyn KObject>; 242*a03c4f9dSLoGin return sysfs_instance().create_file(&kobj, attr); 243*a03c4f9dSLoGin } 244*a03c4f9dSLoGin 245*a03c4f9dSLoGin /// 为指定的驱动删除一个属性文件 246*a03c4f9dSLoGin /// 247*a03c4f9dSLoGin /// 如果属性不存在,也不会报错 248*a03c4f9dSLoGin /// 249*a03c4f9dSLoGin /// ## 参数 250*a03c4f9dSLoGin /// 251*a03c4f9dSLoGin /// - `driver` 要删除属性文件的驱动 252*a03c4f9dSLoGin /// - `attr` 属性 253*a03c4f9dSLoGin pub fn remove_attr_file(&self, driver: &Arc<dyn Driver>, attr: &'static dyn Attribute) { 254*a03c4f9dSLoGin let kobj = driver.clone() as Arc<dyn KObject>; 255*a03c4f9dSLoGin sysfs_instance().remove_file(&kobj, attr); 256*a03c4f9dSLoGin } 257*a03c4f9dSLoGin } 258*a03c4f9dSLoGin 259*a03c4f9dSLoGin /// 驱动匹配器 260*a03c4f9dSLoGin /// 261*a03c4f9dSLoGin /// 用于匹配驱动是否符合某个条件 262*a03c4f9dSLoGin /// 263*a03c4f9dSLoGin /// ## 参数 264*a03c4f9dSLoGin /// 265*a03c4f9dSLoGin /// - `T` - 匹配器的数据类型 266*a03c4f9dSLoGin /// - `data` - 匹配器的数据 267*a03c4f9dSLoGin pub trait DriverMatcher<T>: Debug { 268*a03c4f9dSLoGin fn match_driver(&self, driver: &Arc<dyn Driver>, data: T) -> bool; 269*a03c4f9dSLoGin } 270*a03c4f9dSLoGin 271*a03c4f9dSLoGin /// 根据名称匹配驱动 272*a03c4f9dSLoGin #[derive(Debug)] 273*a03c4f9dSLoGin pub struct DriverMatchName; 274*a03c4f9dSLoGin 275*a03c4f9dSLoGin impl DriverMatcher<&str> for DriverMatchName { 276*a03c4f9dSLoGin #[inline(always)] 277*a03c4f9dSLoGin fn match_driver(&self, driver: &Arc<dyn Driver>, data: &str) -> bool { 278*a03c4f9dSLoGin driver.name() == data 279*a03c4f9dSLoGin } 280*a03c4f9dSLoGin } 281*a03c4f9dSLoGin 282*a03c4f9dSLoGin /// enum probe_type - device driver probe type to try 283*a03c4f9dSLoGin /// Device drivers may opt in for special handling of their 284*a03c4f9dSLoGin /// respective probe routines. This tells the core what to 285*a03c4f9dSLoGin /// expect and prefer. 286*a03c4f9dSLoGin /// 287*a03c4f9dSLoGin /// Note that the end goal is to switch the kernel to use asynchronous 288*a03c4f9dSLoGin /// probing by default, so annotating drivers with 289*a03c4f9dSLoGin /// %PROBE_PREFER_ASYNCHRONOUS is a temporary measure that allows us 290*a03c4f9dSLoGin /// to speed up boot process while we are validating the rest of the 291*a03c4f9dSLoGin /// drivers. 292*a03c4f9dSLoGin #[allow(dead_code)] 293*a03c4f9dSLoGin #[derive(Debug)] 294*a03c4f9dSLoGin pub enum DriverProbeType { 295*a03c4f9dSLoGin /// Used by drivers that work equally well 296*a03c4f9dSLoGin /// whether probed synchronously or asynchronously. 297*a03c4f9dSLoGin DefaultStrategy, 298*a03c4f9dSLoGin 299*a03c4f9dSLoGin /// Drivers for "slow" devices which 300*a03c4f9dSLoGin /// probing order is not essential for booting the system may 301*a03c4f9dSLoGin /// opt into executing their probes asynchronously. 302*a03c4f9dSLoGin PreferAsync, 303*a03c4f9dSLoGin 304*a03c4f9dSLoGin /// Use this to annotate drivers that need 305*a03c4f9dSLoGin /// their probe routines to run synchronously with driver and 306*a03c4f9dSLoGin /// device registration (with the exception of -EPROBE_DEFER 307*a03c4f9dSLoGin /// handling - re-probing always ends up being done asynchronously). 308*a03c4f9dSLoGin ForceSync, 309*a03c4f9dSLoGin } 310*a03c4f9dSLoGin 311*a03c4f9dSLoGin impl Default for DriverProbeType { 312*a03c4f9dSLoGin fn default() -> Self { 313*a03c4f9dSLoGin DriverProbeType::DefaultStrategy 314*a03c4f9dSLoGin } 315e0de0fd6STingHuang } 316