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