Skip to content

Commit

Permalink
refactor(device): make name more consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenRuiwei committed Jul 28, 2024
1 parent db7779f commit d524198
Show file tree
Hide file tree
Showing 21 changed files with 69 additions and 69 deletions.
4 changes: 2 additions & 2 deletions driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use core::fmt::{self, Write};

use async_utils::block_on;
use crate_interface::call_interface;
use device_core::{BlockDriverOps, CharDevice, DeviceMajor};
use device_core::{BlockDevice, CharDevice, DeviceMajor};
use manager::DeviceManager;
use memory::PageTable;
use sbi_print::SbiStdout;
Expand Down Expand Up @@ -59,7 +59,7 @@ pub fn init() {
// CHAR_DEVICE.call_once(|| manager.char_device[0].clone());
}

pub static BLOCK_DEVICE: Once<Arc<dyn BlockDriverOps>> = Once::new();
pub static BLOCK_DEVICE: Once<Arc<dyn BlockDevice>> = Once::new();

// fn init_block_device() {
// BLOCK_DEVICE.call_once(|| VirtIOBlkDev::new());
Expand Down
10 changes: 5 additions & 5 deletions driver/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use alloc::{collections::BTreeMap, sync::Arc, vec::Vec};

use arch::interrupts::{disable_interrupt, enable_external_interrupt};
use config::{board, mm::K_SEG_DTB_BEG};
use device_core::{BaseDriverOps, DevId};
use device_core::{Device, DevId};
use log::{info, warn};

use crate::{cpu::CPU, plic::PLIC, println};
Expand All @@ -14,9 +14,9 @@ pub struct DeviceManager {
pub cpus: Vec<CPU>,
/// net device is excluded from `device`. It is owned by `InterfaceWrapper`
/// in `net` module
pub devices: BTreeMap<DevId, Arc<dyn BaseDriverOps>>,
pub devices: BTreeMap<DevId, Arc<dyn Device>>,
/// irq_no -> device.
pub irq_map: BTreeMap<usize, Arc<dyn BaseDriverOps>>,
pub irq_map: BTreeMap<usize, Arc<dyn Device>>,
}

impl DeviceManager {
Expand Down Expand Up @@ -66,11 +66,11 @@ impl DeviceManager {
self.plic.as_ref().unwrap()
}

pub fn get(&self, dev_id: &DevId) -> Option<&Arc<dyn BaseDriverOps>> {
pub fn get(&self, dev_id: &DevId) -> Option<&Arc<dyn Device>> {
self.devices.get(dev_id)
}

pub fn devices(&self) -> &BTreeMap<DevId, Arc<dyn BaseDriverOps>> {
pub fn devices(&self) -> &BTreeMap<DevId, Arc<dyn Device>> {
&self.devices
}

Expand Down
4 changes: 2 additions & 2 deletions driver/src/serial/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use core::{
use async_trait::async_trait;
use async_utils::{block_on, get_waker};
use config::{board::UART_BUF_LEN, mm::VIRT_RAM_OFFSET};
use device_core::{BaseDriverOps, DevId, DeviceMajor, DeviceMeta, DeviceType};
use device_core::{Device, DevId, DeviceMajor, DeviceMeta, DeviceType};
use fdt::Fdt;
use macro_utils::with_methods;
use memory::pte::PTEFlags;
Expand Down Expand Up @@ -83,7 +83,7 @@ impl fmt::Debug for Serial {
}
}

impl BaseDriverOps for Serial {
impl Device for Serial {
fn meta(&self) -> &DeviceMeta {
&self.meta
}
Expand Down
4 changes: 2 additions & 2 deletions driver/src/virtio/loopback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use alloc::{boxed::Box, collections::VecDeque, vec, vec::Vec};

use device_core::{
error::{DevError, DevResult},
DeviceCapabilities, EthernetAddress, Medium, NetBufPtrOps, NetDriverOps,
DeviceCapabilities, EthernetAddress, Medium, NetBufPtrOps, NetDevice,
};

/// The loopback interface operates at the network layer and handles the packets
Expand All @@ -21,7 +21,7 @@ impl LoopbackDev {
}
}

impl NetDriverOps for LoopbackDev {
impl NetDevice for LoopbackDev {
#[inline]
fn capabilities(&self) -> DeviceCapabilities {
let mut cap = DeviceCapabilities::default();
Expand Down
4 changes: 2 additions & 2 deletions driver/src/virtio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub mod virtio_net;
use core::ptr::NonNull;

use config::mm::VIRT_RAM_OFFSET;
use device_core::{error::DevError, BaseDriverOps};
use device_core::{error::DevError, Device};
use fdt::Fdt;
use log::{error, warn};
use loopback::LoopbackDev;
Expand All @@ -19,7 +19,7 @@ use virtio_drivers::{
},
BufferDirection,
};
use virtio_net::NetDevice;
use virtio_net::NetDeviceImpl;

use crate::{kernel_page_table_mut, manager::DeviceManager, BLOCK_DEVICE};

Expand Down
8 changes: 4 additions & 4 deletions driver/src/virtio/virtio_blk.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use alloc::{string::ToString, sync::Arc};

use config::board::BLOCK_SIZE;
use device_core::{BaseDriverOps, BlockDriverOps, DevId, DeviceMajor, DeviceMeta, DeviceType};
use device_core::{BlockDevice, DevId, Device, DeviceMajor, DeviceMeta, DeviceType};
use log::error;
use page::BufferCache;
use sync::mutex::SpinNoIrqLock;
use virtio_drivers::{device::blk::VirtIOBlk, transport::mmio::MmioTransport};

use super::VirtioHalImpl;

pub type BlockDevice = VirtIoBlkDev;
pub type BlockDeviceImpl = VirtIoBlkDev;

pub struct VirtIoBlkDev {
meta: DeviceMeta,
Expand All @@ -20,7 +20,7 @@ pub struct VirtIoBlkDev {
unsafe impl Send for VirtIoBlkDev {}
unsafe impl Sync for VirtIoBlkDev {}

impl BlockDriverOps for VirtIoBlkDev {
impl BlockDevice for VirtIoBlkDev {
// TODO: cached size value
fn size(&self) -> u64 {
self.device.lock().capacity() * (BLOCK_SIZE as u64)
Expand Down Expand Up @@ -106,7 +106,7 @@ impl VirtIoBlkDev {
}
}

impl BaseDriverOps for VirtIoBlkDev {
impl Device for VirtIoBlkDev {
fn meta(&self) -> &device_core::DeviceMeta {
&self.meta
}
Expand Down
6 changes: 3 additions & 3 deletions driver/src/virtio/virtio_net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::{any::Any, ptr::NonNull};

use device_core::{
error::{DevError, DevResult},
DeviceCapabilities, EthernetAddress, Medium, NetBufPtrOps, NetDriverOps,
DeviceCapabilities, EthernetAddress, Medium, NetBufPtrOps, NetDevice,
};
use virtio_drivers::{
device::net::VirtIONetRaw,
Expand All @@ -13,7 +13,7 @@ use virtio_drivers::{
use super::{as_dev_err, VirtioHalImpl};
use crate::net::{NetBuf, NetBufBox, NetBufPool, NET_BUF_LEN};

pub type NetDevice = VirtIoNetDev<MmioTransport, 32>;
pub type NetDeviceImpl = VirtIoNetDev<MmioTransport, 32>;

/// The VirtIO network device driver.
///
Expand Down Expand Up @@ -79,7 +79,7 @@ impl<T: Transport, const QS: usize> VirtIoNetDev<T, QS> {
}
}

impl<T: Transport + 'static, const QS: usize> NetDriverOps for VirtIoNetDev<T, QS> {
impl<T: Transport + 'static, const QS: usize> NetDevice for VirtIoNetDev<T, QS> {
#[inline]
fn capabilities(&self) -> DeviceCapabilities {
let mut cap = DeviceCapabilities::default();
Expand Down
12 changes: 6 additions & 6 deletions modules/device-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub struct DeviceMeta {
pub dtype: DeviceType,
}

pub trait BaseDriverOps: Sync + Send + DowncastSync {
pub trait Device: Sync + Send + DowncastSync {
fn meta(&self) -> &DeviceMeta;

fn init(&self);
Expand Down Expand Up @@ -84,17 +84,17 @@ pub trait BaseDriverOps: Sync + Send + DowncastSync {
}
}

impl_downcast!(sync BaseDriverOps);
impl_downcast!(sync Device);

#[async_trait]
pub trait CharDevice: BaseDriverOps {
pub trait CharDevice: Device {
async fn read(&self, buf: &mut [u8]) -> usize;
async fn write(&self, buf: &[u8]) -> usize;
async fn poll_in(&self) -> bool;
async fn poll_out(&self) -> bool;
}

pub trait BlockDriverOps: BaseDriverOps {
pub trait BlockDevice: Device {
fn size(&self) -> u64;

fn block_size(&self) -> usize;
Expand All @@ -116,13 +116,13 @@ pub trait BlockDriverOps: BaseDriverOps {
fn write_block(&self, block_id: usize, buf: &[u8]);
}

impl_downcast!(sync BlockDriverOps);
impl_downcast!(sync BlockDevice);

/// The ethernet address of the NIC (MAC address).
pub struct EthernetAddress(pub [u8; 6]);

/// Every Net Device should implement this trait
pub trait NetDriverOps: Sync + Send {
pub trait NetDevice: Sync + Send {
fn capabilities(&self) -> DeviceCapabilities;
/// The ethernet address of the NIC.
fn mac_address(&self) -> EthernetAddress;
Expand Down
6 changes: 3 additions & 3 deletions modules/ext4/src/disk.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use alloc::sync::Arc;

use config::board::BLOCK_SIZE;
use device_core::BlockDriverOps;
use device_core::BlockDevice;
use lwext4_rust::{
bindings::{SEEK_CUR, SEEK_END, SEEK_SET},
KernelDevOp,
Expand All @@ -12,12 +12,12 @@ use systype::SysResult;
pub struct Disk {
block_id: usize,
offset: usize,
dev: Arc<dyn BlockDriverOps>,
dev: Arc<dyn BlockDevice>,
}

impl Disk {
/// Create a new disk.
pub fn new(dev: Arc<dyn BlockDriverOps>) -> Self {
pub fn new(dev: Arc<dyn BlockDevice>) -> Self {
assert_eq!(BLOCK_SIZE, dev.block_size());
Self {
block_id: 0,
Expand Down
4 changes: 2 additions & 2 deletions modules/ext4/src/fs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use alloc::sync::Arc;

use device_core::BlockDriverOps;
use device_core::BlockDevice;
use lwext4_rust::{Ext4BlockWrapper, InodeTypes};
use systype::SysResult;
use vfs_core::{
Expand Down Expand Up @@ -31,7 +31,7 @@ impl FileSystemType for Ext4FsType {
name: &str,
parent: Option<Arc<dyn Dentry>>,
_flags: MountFlags,
dev: Option<Arc<dyn BlockDriverOps>>,
dev: Option<Arc<dyn BlockDevice>>,
) -> SysResult<Arc<dyn Dentry>> {
debug_assert!(dev.is_some());
let sb = Ext4SuperBlock::new(SuperBlockMeta::new(dev, self.clone()));
Expand Down
4 changes: 2 additions & 2 deletions modules/fat32/src/fs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use alloc::sync::Arc;

use device_core::BlockDriverOps;
use device_core::BlockDevice;
use vfs_core::{Dentry, FileSystemType, FileSystemTypeMeta, StatFs, SuperBlock, SuperBlockMeta};

use crate::{as_sys_err, dentry::FatDentry, inode::dir::FatDirInode, DiskCursor, FatFs};
Expand All @@ -27,7 +27,7 @@ impl FileSystemType for FatFsType {
name: &str,
parent: Option<Arc<dyn Dentry>>,
_flags: vfs_core::MountFlags,
dev: Option<Arc<dyn BlockDriverOps>>,
dev: Option<Arc<dyn BlockDevice>>,
) -> systype::SysResult<Arc<dyn vfs_core::Dentry>> {
debug_assert!(dev.is_some());
let sb = FatSuperBlock::new(SuperBlockMeta::new(dev, self.clone()));
Expand Down
4 changes: 2 additions & 2 deletions modules/fat32/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use alloc::sync::Arc;

use device_core::BlockDriverOps;
use device_core::BlockDevice;
use fatfs::{DefaultTimeProvider, Dir, DirIter, Error, File, FileSystem, LossyOemCpConverter};
use sync::mutex::SpinNoIrqLock;
use systype::SysError;
Expand Down Expand Up @@ -42,7 +42,7 @@ pub const fn as_sys_err(err: fatfs::Error<()>) -> systype::SysError {
pub struct DiskCursor {
sector: u64,
offset: usize,
blk_dev: Arc<dyn BlockDriverOps>,
blk_dev: Arc<dyn BlockDevice>,
}

impl DiskCursor {
Expand Down
14 changes: 7 additions & 7 deletions modules/net/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use core::{cell::RefCell, future::Future, ops::DerefMut, panic};

use arch::time::get_time_us;
use crate_interface::call_interface;
use device_core::{error::DevError, NetBufPtrOps, NetDriverOps};
use device_core::{error::DevError, NetBufPtrOps, NetDevice};
use listen_table::*;
use log::*;
pub use smoltcp::wire::{IpAddress, IpEndpoint, IpListenEndpoint, Ipv4Address, Ipv6Address};
Expand Down Expand Up @@ -65,7 +65,7 @@ static ETH0: Once<InterfaceWrapper> = Once::new();
struct SocketSetWrapper<'a>(Mutex<SocketSet<'a>>);

struct DeviceWrapper {
inner: RefCell<Box<dyn NetDriverOps>>, /* use `RefCell` is enough since it's wrapped in
inner: RefCell<Box<dyn NetDevice>>, /* use `RefCell` is enough since it's wrapped in
* `Mutex` in
* `InterfaceWrapper`. */
}
Expand Down Expand Up @@ -175,7 +175,7 @@ impl<'a> SocketSetWrapper<'a> {
}

impl InterfaceWrapper {
fn new(name: &'static str, dev: Box<dyn NetDriverOps>, ether_addr: EthernetAddress) -> Self {
fn new(name: &'static str, dev: Box<dyn NetDevice>, ether_addr: EthernetAddress) -> Self {
// let mut config = Config::new(HardwareAddress::Ethernet(ether_addr));
// let mut config = if ether_addr == EthernetAddress([0, 0, 0, 0, 0, 0]) {
// log::error!("[InterfaceWrapper] use HardwareAddress::Ip");
Expand Down Expand Up @@ -237,7 +237,7 @@ impl InterfaceWrapper {
}

impl DeviceWrapper {
fn new(inner: Box<dyn NetDriverOps>) -> Self {
fn new(inner: Box<dyn NetDevice>) -> Self {
Self {
inner: RefCell::new(inner),
}
Expand Down Expand Up @@ -288,8 +288,8 @@ impl Device for DeviceWrapper {
}
}

struct NetRxToken<'a>(&'a RefCell<Box<dyn NetDriverOps>>, Box<dyn NetBufPtrOps>);
struct NetTxToken<'a>(&'a RefCell<Box<dyn NetDriverOps>>);
struct NetRxToken<'a>(&'a RefCell<Box<dyn NetDevice>>, Box<dyn NetBufPtrOps>);
struct NetTxToken<'a>(&'a RefCell<Box<dyn NetDevice>>);

impl<'a> RxToken for NetRxToken<'a> {
fn preprocess(&self, sockets: &mut SocketSet<'_>) {
Expand Down Expand Up @@ -416,7 +416,7 @@ pub const SEND_SHUTDOWN: u8 = 2;
/// 表示读和写方向都已关闭(相当于SHUT_RDWR)
pub const SHUTDOWN_MASK: u8 = 3;

pub fn init_network(net_dev: Box<dyn NetDriverOps>, is_loopback: bool) {
pub fn init_network(net_dev: Box<dyn NetDevice>, is_loopback: bool) {
info!("Initialize network subsystem...");
let ether_addr = EthernetAddress(net_dev.mac_address().0);
let eth0 = InterfaceWrapper::new("eth0", net_dev, ether_addr);
Expand Down
8 changes: 4 additions & 4 deletions modules/page/src/buffer_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use config::{
MAX_BUFFER_PAGES, PAGE_SIZE,
},
};
use device_core::BlockDriverOps;
use device_core::BlockDevice;
use intrusive_collections::{intrusive_adapter, LinkedListAtomicLink};
use lru::LruCache;
use macro_utils::with_methods;
Expand All @@ -17,7 +17,7 @@ use sync::mutex::SpinNoIrqLock;
use crate::Page;

pub struct BufferCache {
device: Option<Weak<dyn BlockDriverOps>>,
device: Option<Weak<dyn BlockDevice>>,
/// Block page id to `Page`.
// NOTE: These `Page`s are pages without file, only exist for caching pure
// block data.
Expand All @@ -39,11 +39,11 @@ impl BufferCache {
}
}

pub fn init_device(&mut self, device: Arc<dyn BlockDriverOps>) {
pub fn init_device(&mut self, device: Arc<dyn BlockDevice>) {
self.device = Some(Arc::downgrade(&device))
}

pub fn device(&self) -> Arc<dyn BlockDriverOps> {
pub fn device(&self) -> Arc<dyn BlockDevice> {
self.device.as_ref().unwrap().upgrade().unwrap()
}

Expand Down
Loading

0 comments on commit d524198

Please sign in to comment.