1 use alloc::{sync::Arc, vec::Vec}; 2 use system_error::SystemError; 3 4 use crate::driver::base::device::{ 5 bus::Bus, 6 driver::{driver_manager, Driver}, 7 }; 8 9 use super::{dev_id::PciDeviceID, device::PciDevice, subsys::pci_bus}; 10 11 /// # trait功能 12 /// Pci驱动应该实现的trait 13 /// 14 /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/include/linux/pci.h#907 15 pub trait PciDriver: Driver { 16 /// # 函数的功能 17 /// 对设备进行probe操作 18 /// 19 /// ## 参数: 20 /// - 'device' :要进行probe的设备 21 /// - 'id' :设备的ID(暂时不清楚为什么需要这个,依Linux实现是有ID的) 22 /// 23 /// ## 返回值: 24 /// - Ok:probe成功 25 /// - Err:probe失败 26 fn probe(&self, device: &Arc<dyn PciDevice>, id: &PciDeviceID) -> Result<(), SystemError>; 27 fn remove(&self, device: &Arc<dyn PciDevice>) -> Result<(), SystemError>; 28 fn shutdown(&self, device: &Arc<dyn PciDevice>) -> Result<(), SystemError>; 29 fn suspend(&self, device: &Arc<dyn PciDevice>) -> Result<(), SystemError>; 30 fn resume(&self, device: &Arc<dyn PciDevice>) -> Result<(), SystemError>; 31 /// # 函数的功能 32 /// 向驱动中加入一个PciDeviceID,表示该驱动可以支持该ID的设备 33 /// 34 /// ## 参数: 35 /// - 'id' :要添加的ID 36 /// 37 /// ## 返回值: 38 /// - 'Ok':添加成功 39 /// - 'Err':添加失败 40 fn add_dynid(&mut self, id: PciDeviceID) -> Result<(), SystemError>; 41 /// # 函数的功能 42 /// 每个Pci驱动都应该持有一个支持ID的列表,并通过该函数进行访问 43 /// 44 /// ## 返回值: 45 /// - 'Some(Vec)': 支持ID的列表 46 /// - 'None':未能获取列表 47 fn locked_dynid_list(&self) -> Option<Vec<Arc<PciDeviceID>>>; 48 /// # 函数的功能 49 /// 检测当前驱动是否支持目标设备 50 /// 51 /// ## 参数: 52 /// - 'dev' :要检测的设备 53 /// 54 /// ## 返回值: 55 /// - 'Some(Arc<PciDeviceID>)': 如果支持,则返回支持的ID 56 /// - 'None': 不支持的设备 57 fn match_dev(&self, dev: &Arc<dyn PciDevice>) -> Option<Arc<PciDeviceID>> { 58 for i in self.locked_dynid_list()?.iter() { 59 if i.match_dev(dev) { 60 return Some(i.clone()); 61 } 62 } 63 return None; 64 } 65 } 66 67 pub struct PciDriverManager; 68 69 pub fn pci_driver_manager() -> &'static PciDriverManager { 70 &PciDriverManager 71 } 72 73 impl PciDriverManager { 74 pub fn register(&self, driver: Arc<dyn PciDriver>) -> Result<(), SystemError> { 75 driver.set_bus(Some(Arc::downgrade(&(pci_bus() as Arc<dyn Bus>)))); 76 return driver_manager().register(driver as Arc<dyn Driver>); 77 } 78 79 #[allow(dead_code)] 80 pub fn unregister(&self, driver: &Arc<dyn PciDriver>) { 81 driver_manager().unregister(&(driver.clone() as Arc<dyn Driver>)); 82 } 83 } 84