2019-01-23 18:11:41 +04:00
|
|
|
use alloc::prelude::*;
|
2019-03-06 21:00:46 +04:00
|
|
|
use alloc::sync::Arc;
|
2019-01-23 18:11:41 +04:00
|
|
|
|
|
|
|
use lazy_static::lazy_static;
|
2019-03-02 20:44:46 +04:00
|
|
|
use smoltcp::wire::{EthernetAddress, Ipv4Address};
|
2019-02-28 08:31:10 +04:00
|
|
|
use smoltcp::socket::SocketSet;
|
2019-03-06 21:00:46 +04:00
|
|
|
use spin::RwLock;
|
2019-01-23 18:11:41 +04:00
|
|
|
|
2019-03-06 21:00:46 +04:00
|
|
|
use crate::sync::{Condvar, MutexGuard, SpinNoIrq};
|
2019-03-12 07:49:17 +04:00
|
|
|
use self::block::virtio_blk::VirtIOBlkDriver;
|
2019-01-23 18:11:41 +04:00
|
|
|
|
|
|
|
mod device_tree;
|
|
|
|
pub mod bus;
|
|
|
|
pub mod net;
|
2019-01-25 06:28:38 +04:00
|
|
|
pub mod block;
|
2019-01-23 18:11:41 +04:00
|
|
|
mod gpu;
|
2019-01-25 06:28:38 +04:00
|
|
|
mod input;
|
2019-01-23 18:11:41 +04:00
|
|
|
|
2019-03-12 07:49:17 +04:00
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
2019-01-23 18:11:41 +04:00
|
|
|
pub enum DeviceType {
|
|
|
|
Net,
|
2019-01-25 06:28:38 +04:00
|
|
|
Gpu,
|
|
|
|
Input,
|
|
|
|
Block
|
2019-01-23 18:11:41 +04:00
|
|
|
}
|
|
|
|
|
2019-03-06 21:00:46 +04:00
|
|
|
pub trait Driver : Send + Sync {
|
2019-01-23 18:11:41 +04:00
|
|
|
// if interrupt belongs to this driver, handle it and return true
|
|
|
|
// return false otherwise
|
2019-03-06 21:00:46 +04:00
|
|
|
fn try_handle_interrupt(&self) -> bool;
|
2019-01-23 18:11:41 +04:00
|
|
|
|
|
|
|
// return the correspondent device type, see DeviceType
|
|
|
|
fn device_type(&self) -> DeviceType;
|
|
|
|
}
|
|
|
|
|
2019-03-06 21:00:46 +04:00
|
|
|
pub trait NetDriver : Driver {
|
2019-01-23 18:11:41 +04:00
|
|
|
// get mac address for this device
|
|
|
|
fn get_mac(&self) -> EthernetAddress;
|
|
|
|
|
|
|
|
// get interface name for this device
|
|
|
|
fn get_ifname(&self) -> String;
|
2019-02-28 08:31:10 +04:00
|
|
|
|
2019-03-02 20:44:46 +04:00
|
|
|
// get ipv4 address
|
|
|
|
fn ipv4_address(&self) -> Option<Ipv4Address>;
|
|
|
|
|
2019-03-04 11:34:02 +04:00
|
|
|
// get sockets
|
2019-03-06 21:00:46 +04:00
|
|
|
fn sockets(&self) -> MutexGuard<SocketSet<'static, 'static, 'static>, SpinNoIrq>;
|
2019-03-04 11:34:02 +04:00
|
|
|
|
|
|
|
// manually trigger a poll, use it after sending packets
|
2019-03-06 21:00:46 +04:00
|
|
|
fn poll(&self);
|
2019-01-23 18:11:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lazy_static! {
|
2019-03-06 21:00:46 +04:00
|
|
|
// NOTE: RwLock only write when initializing drivers
|
|
|
|
pub static ref DRIVERS: RwLock<Vec<Arc<Driver>>> = RwLock::new(Vec::new());
|
|
|
|
pub static ref NET_DRIVERS: RwLock<Vec<Arc<NetDriver>>> = RwLock::new(Vec::new());
|
2019-03-12 07:49:17 +04:00
|
|
|
pub static ref BLK_DRIVERS: RwLock<Vec<Arc<VirtIOBlkDriver>>> = RwLock::new(Vec::new());
|
2019-01-23 18:11:41 +04:00
|
|
|
}
|
|
|
|
|
2019-03-06 16:24:55 +04:00
|
|
|
lazy_static!{
|
2019-03-04 11:34:02 +04:00
|
|
|
pub static ref SOCKET_ACTIVITY: Condvar = Condvar::new();
|
|
|
|
}
|
|
|
|
|
2019-02-25 15:12:41 +04:00
|
|
|
#[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
|
2019-01-23 18:11:41 +04:00
|
|
|
pub fn init(dtb: usize) {
|
|
|
|
device_tree::init(dtb);
|
2019-02-25 15:12:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
pub fn init() {
|
|
|
|
bus::pci::init();
|
2019-01-23 18:11:41 +04:00
|
|
|
}
|