kernel/device/
mod.rs

1//! Device module.
2//! 
3//! This module provides a framework for managing devices in the kernel.
4//! It includes device information and driver management,
5//! as well as platform-specific device handling.
6
7
8pub mod manager;
9pub mod fdt;
10pub mod platform;
11pub mod block;
12pub mod char;
13pub mod events;
14
15extern crate alloc;
16use core::any::Any;
17
18use alloc::vec::Vec;
19
20pub trait DeviceInfo {
21    fn name(&self) -> &'static str;
22    fn id(&self) -> usize;
23    fn compatible(&self) -> Vec<&'static str>;
24    fn as_any(&self) -> &dyn Any;
25}
26
27/// Device driver trait.
28/// 
29/// This trait defines the interface for device drivers in the kernel.
30/// It includes methods for getting the driver's name,
31/// matching the driver to devices, and handling device probing and removal.
32pub trait DeviceDriver {
33    fn name(&self) -> &'static str;
34    fn match_table(&self) -> Vec<&'static str>;
35    fn probe(&self, device: &dyn DeviceInfo) -> Result<(), &'static str>;
36    fn remove(&self, device: &dyn DeviceInfo) -> Result<(), &'static str>;
37}
38
39/// Device type enumeration.
40/// 
41/// This enum defines the types of devices that can be managed by the kernel.
42/// It includes block devices, character devices, network devices,
43/// and generic devices.
44/// 
45#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
46pub enum DeviceType {
47    Block,
48    Char,
49    Network,
50    Generic,
51    #[cfg(test)]
52    NonExistent,
53}
54
55/// Device trait.
56/// 
57/// This trait defines the interface for devices in the kernel.
58/// Device IDs are assigned by DeviceManager when devices are registered.
59/// 
60pub trait Device: Send + Sync {
61    fn device_type(&self) -> DeviceType;
62    fn name(&self) -> &'static str;
63    fn as_any(&self) -> &dyn Any;
64    fn as_any_mut(&mut self) -> &mut dyn Any;
65    
66    /// Cast to CharDevice if this device is a character device
67    fn as_char_device(&self) -> Option<&dyn char::CharDevice> {
68        None
69    }
70    
71    /// Cast to BlockDevice if this device is a block device  
72    fn as_block_device(&self) -> Option<&dyn block::BlockDevice> {
73        None
74    }
75}
76
77pub struct GenericDevice {
78    device_type: DeviceType,
79    name: &'static str,
80}
81
82impl GenericDevice {
83    pub fn new(name: &'static str) -> Self {
84        Self { device_type: DeviceType::Generic, name }
85    }
86}
87
88impl Device for GenericDevice {
89    fn device_type(&self) -> DeviceType {
90        self.device_type
91    }
92
93    fn name(&self) -> &'static str {
94        self.name
95    }
96
97    fn as_any(&self) -> &dyn Any {
98        self
99    }
100    
101    fn as_any_mut(&mut self) -> &mut dyn Any {
102        self
103    }
104}