Skip to content

Commit

Permalink
完善设备驱动模型,基于kset、kobj来维护对象之间的关系 (DragonOS-Community#401)
Browse files Browse the repository at this point in the history
* 使用kobj和kset管理/sys文件夹下的对象

* 修改notifier,把action从u64换为泛型。

* 完善设备驱动模型,基于kset、kobj来维护对象之间的关系
  • Loading branch information
fslongjin authored Oct 10, 2023
1 parent 6abb8bd commit 06d5e24
Show file tree
Hide file tree
Showing 46 changed files with 3,465 additions and 1,503 deletions.
8 changes: 8 additions & 0 deletions kernel/src/driver/acpi/glue.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use alloc::sync::Arc;

use crate::driver::base::device::Device;

/// 参考: https://opengrok.ringotek.cn/xref/linux-6.1.9/drivers/acpi/glue.c#352
pub fn acpi_device_notify(_dev: &Arc<dyn Device>) {
return;
}
1 change: 1 addition & 0 deletions kernel/src/driver/acpi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::{
};

mod c_adapter;
pub mod glue;
pub mod old;

extern crate acpi;
Expand Down
7 changes: 4 additions & 3 deletions kernel/src/driver/base/block/block_device.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// 引入Module
use crate::{
driver::base::{
device::{mkdev, Device, DeviceNumber, IdTable, BLOCKDEVS, DEVICE_MANAGER},
device::{mkdev, Device, DeviceError, DeviceNumber, IdTable, BLOCKDEVS},
map::{
DeviceStruct, DEV_MAJOR_DYN_END, DEV_MAJOR_DYN_EXT_END, DEV_MAJOR_DYN_EXT_START,
DEV_MAJOR_HASH_SIZE, DEV_MAJOR_MAX, MINOR_MASK,
Expand Down Expand Up @@ -475,11 +475,12 @@ impl BlockDeviceOps {
/// range: 次设备号范围
/// @return: none
#[allow(dead_code)]
pub fn bdev_add(bdev: Arc<dyn BlockDevice>, id_table: IdTable) {
pub fn bdev_add(_bdev: Arc<dyn BlockDevice>, id_table: IdTable) -> Result<(), DeviceError> {
if Into::<usize>::into(id_table.device_number()) == 0 {
kerror!("Device number can't be 0!\n");
}
DEVICE_MANAGER.add_device(id_table, bdev.device())
todo!("bdev_add")
// return device_manager().add_device(bdev.id_table(), bdev.device());
}

/// @brief: block设备注销
Expand Down
10 changes: 10 additions & 0 deletions kernel/src/driver/base/c_adapter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use super::init::driver_init;

#[no_mangle]
unsafe extern "C" fn rs_driver_init() -> i32 {
let result = driver_init()
.map(|_| 0)
.unwrap_or_else(|e| e.to_posix_errno());

return result;
}
16 changes: 11 additions & 5 deletions kernel/src/driver/base/char/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use alloc::sync::Arc;
use crate::{kerror, syscall::SystemError};

use super::{
device::{mkdev, Device, DeviceNumber, IdTable, CHARDEVS, DEVICE_MANAGER, DEVMAP},
device::{device_manager, mkdev, Device, DeviceNumber, IdTable, CHARDEVS, DEVMAP},
map::{
kobj_map, kobj_unmap, DeviceStruct, DEV_MAJOR_DYN_END, DEV_MAJOR_DYN_EXT_END,
DEV_MAJOR_DYN_EXT_START, DEV_MAJOR_HASH_SIZE, DEV_MAJOR_MAX, MINOR_MASK,
Expand Down Expand Up @@ -189,17 +189,23 @@ impl CharDevOps {
/// range: 次设备号范围
/// @return: none
#[allow(dead_code)]
pub fn cdev_add(cdev: Arc<dyn CharDevice>, id_table: IdTable, range: usize) {
pub fn cdev_add(
cdev: Arc<dyn CharDevice>,
id_table: IdTable,
range: usize,
) -> Result<(), SystemError> {
if Into::<usize>::into(id_table.device_number()) == 0 {
kerror!("Device number can't be 0!\n");
}
DEVICE_MANAGER.add_device(id_table.clone(), cdev.clone());
device_manager().add_device(cdev.clone())?;
kobj_map(
DEVMAP.clone(),
id_table.device_number(),
range,
cdev.clone(),
)
);

return Ok(());
}

/// @brief: 字符设备注销
Expand All @@ -208,7 +214,7 @@ impl CharDevOps {
/// @return: none
#[allow(dead_code)]
pub fn cdev_del(id_table: IdTable, range: usize) {
DEVICE_MANAGER.remove_device(&id_table);
device_manager().remove_device(&id_table);
kobj_unmap(DEVMAP.clone(), id_table.device_number(), range);
}
}
26 changes: 26 additions & 0 deletions kernel/src/driver/base/class.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use alloc::{string::ToString, sync::Arc};

use crate::syscall::SystemError;

use super::kset::KSet;

/// `/sys/class`的kset
static mut CLASS_KSET_INSTANCE: Option<Arc<KSet>> = None;

#[inline(always)]
#[allow(dead_code)]
pub fn sys_class_kset() -> Arc<KSet> {
unsafe { CLASS_KSET_INSTANCE.clone().unwrap() }
}

/// 初始化`/sys/class`的kset
pub(super) fn classes_init() -> Result<(), SystemError> {
let class_kset = KSet::new("class".to_string());
class_kset
.register(None)
.expect("register class kset failed");
unsafe {
CLASS_KSET_INSTANCE = Some(class_kset);
}
return Ok(());
}
Loading

0 comments on commit 06d5e24

Please sign in to comment.