1
0
mirror of https://github.com/rcore-os/rCore.git synced 2024-11-22 08:06:17 +04:00

Serial device file for user using 2nd UART.

This commit is contained in:
gjz010 2021-05-11 02:09:52 +08:00
parent 20b1bc534d
commit 2d732a48d8
3 changed files with 85 additions and 1 deletions

View File

@ -2,10 +2,12 @@
mod fbdev; mod fbdev;
mod random; mod random;
mod serial;
mod shm; mod shm;
mod tty; mod tty;
pub use fbdev::*; pub use fbdev::*;
pub use random::*; pub use random::*;
pub use serial::*;
pub use shm::*; pub use shm::*;
pub use tty::*; pub use tty::*;

View File

@ -0,0 +1,78 @@
use crate::drivers::serial::SERIAL_ACTIVITY;
use crate::drivers::SerialDriver;
use crate::drivers::SERIAL_DRIVERS;
use crate::syscall::spin_and_wait;
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::any::Any;
use rcore_fs::vfs::*;
pub struct Serial {
id: usize,
driver: Arc<dyn SerialDriver>,
}
impl Serial {
pub fn new(id: usize, driver: Arc<dyn SerialDriver>) -> Self {
Serial { id, driver }
}
pub fn wrap_all_serial_devices() -> Vec<Self> {
let drivers = SERIAL_DRIVERS.read();
drivers
.iter()
.cloned()
.enumerate()
.map(|(i, x)| Serial::new(i, x))
.collect()
}
}
impl INode for Serial {
fn read_at(&self, _offset: usize, buf: &mut [u8]) -> Result<usize> {
let mut n = 0;
for r in buf.iter_mut() {
if let Some(x) = self.driver.try_read() {
*r = x;
n += 1;
} else {
break;
}
}
Ok(n)
}
fn write_at(&self, _offset: usize, buf: &[u8]) -> Result<usize> {
self.driver.write(buf);
Ok(buf.len())
}
fn poll(&self) -> Result<PollStatus> {
Ok(PollStatus {
read: true,
write: true,
error: false,
})
}
fn metadata(&self) -> Result<Metadata> {
Ok(Metadata {
dev: 1,
inode: 1,
size: 0,
blk_size: 0,
blocks: 0,
atime: Timespec { sec: 0, nsec: 0 },
mtime: Timespec { sec: 0, nsec: 0 },
ctime: Timespec { sec: 0, nsec: 0 },
type_: FileType::CharDevice,
mode: 0o666,
nlinks: 1,
uid: 0,
gid: 0,
rdev: make_rdev(4, self.id),
})
}
fn as_any_ref(&self) -> &dyn Any {
self
}
}

View File

@ -11,7 +11,7 @@ use rcore_fs_sfs::{INodeImpl, SimpleFileSystem};
use self::devfs::{Fbdev, RandomINode}; use self::devfs::{Fbdev, RandomINode};
pub use self::devfs::{ShmINode, TTY}; pub use self::devfs::{Serial, ShmINode, TTY};
pub use self::file::*; pub use self::file::*;
pub use self::file_like::*; pub use self::file_like::*;
pub use self::pipe::Pipe; pub use self::pipe::Pipe;
@ -82,6 +82,10 @@ lazy_static! {
devfs.add("tty", TTY.clone()).expect("failed to mknod /dev/tty"); devfs.add("tty", TTY.clone()).expect("failed to mknod /dev/tty");
devfs.add("fb0", Arc::new(Fbdev::default())).expect("failed to mknod /dev/fb0"); devfs.add("fb0", Arc::new(Fbdev::default())).expect("failed to mknod /dev/fb0");
devfs.add("shm", Arc::new(ShmINode::default())).expect("failed to mkdir shm"); devfs.add("shm", Arc::new(ShmINode::default())).expect("failed to mkdir shm");
for (i, serial) in Serial::wrap_all_serial_devices().into_iter().enumerate(){
devfs.add(&format!("ttyS{}", i), Arc::new(serial)).expect("failed to add a serial");
}
#[cfg(feature = "hypervisor")] #[cfg(feature = "hypervisor")]
devfs.add("rvm", Arc::new(crate::rvm::RvmINode::new())).expect("failed to mknod /dev/rvm"); devfs.add("rvm", Arc::new(crate::rvm::RvmINode::new())).expect("failed to mknod /dev/rvm");