12b7818e8SLoGin use core::sync::atomic::AtomicBool;
2c566df45SLoGin
3c566df45SLoGin use alloc::{
4c566df45SLoGin string::{String, ToString},
5c566df45SLoGin sync::{Arc, Weak},
6c566df45SLoGin vec::Vec,
7c566df45SLoGin };
82eab6dd7S曾俊 use log::{info, warn};
9c566df45SLoGin use system_error::SystemError;
10c566df45SLoGin use unified_init::macros::unified_init;
11c566df45SLoGin
12c566df45SLoGin use crate::{
13c566df45SLoGin driver::{
14c566df45SLoGin base::{
15c566df45SLoGin class::Class,
16c566df45SLoGin device::{
17*28fe4ad2S黄铭涛 bus::Bus, device_manager, driver::Driver, Device, DeviceCommonData, DeviceState,
18*28fe4ad2S黄铭涛 DeviceType, IdTable,
19c566df45SLoGin },
20*28fe4ad2S黄铭涛 kobject::{KObjType, KObject, KObjectCommonData, KObjectState, LockedKObjectState},
21c566df45SLoGin kset::KSet,
22c566df45SLoGin platform::{
23c566df45SLoGin platform_device::{platform_device_manager, PlatformDevice},
24c566df45SLoGin platform_driver::{platform_driver_manager, PlatformDriver},
25c566df45SLoGin },
26c566df45SLoGin },
2752da9a59SGnoCiYeH serial::serial8250::send_to_default_serial8250_port,
2852da9a59SGnoCiYeH video::fbdev::base::{fbmem::frame_buffer_manager, FbVisual, FRAME_BUFFER_SET},
29c566df45SLoGin },
30c566df45SLoGin filesystem::{
31c566df45SLoGin kernfs::KernFSInode,
32c566df45SLoGin sysfs::{file::sysfs_emit_str, Attribute, AttributeGroup, SysFSOpsSupport},
33c566df45SLoGin vfs::syscall::ModeType,
34c566df45SLoGin },
352b7818e8SLoGin init::{boot::boot_callbacks, boot_params, initcall::INITCALL_DEVICE},
36c566df45SLoGin libs::{
37c566df45SLoGin once::Once,
38c566df45SLoGin rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard},
39*28fe4ad2S黄铭涛 spinlock::{SpinLock, SpinLockGuard},
40c566df45SLoGin },
412b7818e8SLoGin mm::{early_ioremap::EarlyIoRemap, PhysAddr, VirtAddr},
42c566df45SLoGin };
43c566df45SLoGin
44c566df45SLoGin use super::base::{
45c566df45SLoGin fbmem::FbDevice, BlankMode, BootTimeVideoType, FbAccel, FbActivateFlags, FbId, FbState, FbType,
46c566df45SLoGin FbVModeFlags, FbVarScreenInfo, FbVideoMode, FixedScreenInfo, FrameBuffer, FrameBufferInfo,
4752da9a59SGnoCiYeH FrameBufferInfoData, FrameBufferOps,
48c566df45SLoGin };
49c566df45SLoGin
50c566df45SLoGin /// 当前机器上面是否有vesa帧缓冲区
51c566df45SLoGin static HAS_VESA_FB: AtomicBool = AtomicBool::new(false);
52c566df45SLoGin
53c566df45SLoGin lazy_static! {
54c566df45SLoGin static ref VESAFB_FIX_INFO: RwLock<FixedScreenInfo> = RwLock::new(FixedScreenInfo {
55c566df45SLoGin id: FixedScreenInfo::name2id("VESA VGA"),
56c566df45SLoGin fb_type: FbType::PackedPixels,
57c566df45SLoGin accel: FbAccel::None,
58c566df45SLoGin ..Default::default()
59c566df45SLoGin });
60c566df45SLoGin static ref VESAFB_DEFINED: RwLock<FbVarScreenInfo> = RwLock::new(FbVarScreenInfo {
61c566df45SLoGin activate: FbActivateFlags::FB_ACTIVATE_NOW,
62c566df45SLoGin height: None,
63c566df45SLoGin width: None,
64c566df45SLoGin right_margin: 32,
65c566df45SLoGin upper_margin: 16,
66c566df45SLoGin lower_margin: 4,
67c566df45SLoGin vsync_len: 4,
68c566df45SLoGin vmode: FbVModeFlags::FB_VMODE_NONINTERLACED,
69c566df45SLoGin
70c566df45SLoGin ..Default::default()
71c566df45SLoGin });
72c566df45SLoGin }
73c566df45SLoGin
74c566df45SLoGin #[derive(Debug)]
75c566df45SLoGin #[cast_to([sync] Device)]
76c566df45SLoGin #[cast_to([sync] PlatformDevice)]
77c566df45SLoGin pub struct VesaFb {
78c566df45SLoGin inner: SpinLock<InnerVesaFb>,
79c566df45SLoGin kobj_state: LockedKObjectState,
8052da9a59SGnoCiYeH fb_data: RwLock<FrameBufferInfoData>,
81c566df45SLoGin }
82c566df45SLoGin
83c566df45SLoGin impl VesaFb {
84c566df45SLoGin pub const NAME: &'static str = "vesa_vga";
new() -> Self85c566df45SLoGin pub fn new() -> Self {
8652da9a59SGnoCiYeH let mut fb_info_data = FrameBufferInfoData::new();
8752da9a59SGnoCiYeH fb_info_data.pesudo_palette.resize(256, 0);
88c566df45SLoGin return Self {
89c566df45SLoGin inner: SpinLock::new(InnerVesaFb {
90*28fe4ad2S黄铭涛 kobject_common: KObjectCommonData::default(),
91*28fe4ad2S黄铭涛 device_common: DeviceCommonData::default(),
92c566df45SLoGin device_state: DeviceState::NotInitialized,
93c566df45SLoGin pdev_id: 0,
94c566df45SLoGin pdev_id_auto: false,
95c566df45SLoGin fb_id: FbId::INIT,
96c566df45SLoGin fb_device: None,
97c566df45SLoGin fb_state: FbState::Suspended,
98c566df45SLoGin }),
99c566df45SLoGin kobj_state: LockedKObjectState::new(None),
10052da9a59SGnoCiYeH fb_data: RwLock::new(fb_info_data),
101c566df45SLoGin };
102c566df45SLoGin }
103*28fe4ad2S黄铭涛
inner(&self) -> SpinLockGuard<InnerVesaFb>104*28fe4ad2S黄铭涛 fn inner(&self) -> SpinLockGuard<InnerVesaFb> {
105*28fe4ad2S黄铭涛 self.inner.lock()
106*28fe4ad2S黄铭涛 }
107c566df45SLoGin }
108c566df45SLoGin
109c566df45SLoGin #[derive(Debug)]
110c566df45SLoGin struct InnerVesaFb {
111*28fe4ad2S黄铭涛 kobject_common: KObjectCommonData,
112*28fe4ad2S黄铭涛 device_common: DeviceCommonData,
113c566df45SLoGin device_state: DeviceState,
114c566df45SLoGin pdev_id: i32,
115c566df45SLoGin pdev_id_auto: bool,
116c566df45SLoGin fb_id: FbId,
117c566df45SLoGin fb_device: Option<Arc<FbDevice>>,
118c566df45SLoGin fb_state: FbState,
119c566df45SLoGin }
120c566df45SLoGin
121c566df45SLoGin impl FrameBuffer for VesaFb {
fb_id(&self) -> FbId122c566df45SLoGin fn fb_id(&self) -> FbId {
123c566df45SLoGin self.inner.lock().fb_id
124c566df45SLoGin }
125c566df45SLoGin
set_fb_id(&self, id: FbId)126c566df45SLoGin fn set_fb_id(&self, id: FbId) {
127c566df45SLoGin self.inner.lock().fb_id = id;
128c566df45SLoGin }
129c566df45SLoGin }
130c566df45SLoGin
131c566df45SLoGin impl PlatformDevice for VesaFb {
pdev_name(&self) -> &str132c566df45SLoGin fn pdev_name(&self) -> &str {
133c566df45SLoGin Self::NAME
134c566df45SLoGin }
135c566df45SLoGin
set_pdev_id(&self, id: i32)136c566df45SLoGin fn set_pdev_id(&self, id: i32) {
137c566df45SLoGin self.inner.lock().pdev_id = id;
138c566df45SLoGin }
139c566df45SLoGin
set_pdev_id_auto(&self, id_auto: bool)140c566df45SLoGin fn set_pdev_id_auto(&self, id_auto: bool) {
141c566df45SLoGin self.inner.lock().pdev_id_auto = id_auto;
142c566df45SLoGin }
143c566df45SLoGin
is_initialized(&self) -> bool144c566df45SLoGin fn is_initialized(&self) -> bool {
145c566df45SLoGin self.inner.lock().device_state == DeviceState::Initialized
146c566df45SLoGin }
147c566df45SLoGin
set_state(&self, set_state: DeviceState)148c566df45SLoGin fn set_state(&self, set_state: DeviceState) {
149c566df45SLoGin self.inner.lock().device_state = set_state;
150c566df45SLoGin }
151c566df45SLoGin }
152c566df45SLoGin
153c566df45SLoGin impl Device for VesaFb {
dev_type(&self) -> DeviceType154c566df45SLoGin fn dev_type(&self) -> DeviceType {
155c566df45SLoGin DeviceType::Char
156c566df45SLoGin }
157c566df45SLoGin
id_table(&self) -> IdTable158c566df45SLoGin fn id_table(&self) -> IdTable {
159c566df45SLoGin IdTable::new(self.name(), None)
160c566df45SLoGin }
161c566df45SLoGin
bus(&self) -> Option<Weak<dyn Bus>>162c566df45SLoGin fn bus(&self) -> Option<Weak<dyn Bus>> {
163*28fe4ad2S黄铭涛 self.inner().device_common.bus.clone()
164c566df45SLoGin }
165c566df45SLoGin
set_bus(&self, bus: Option<Weak<dyn Bus>>)166c566df45SLoGin fn set_bus(&self, bus: Option<Weak<dyn Bus>>) {
167*28fe4ad2S黄铭涛 self.inner().device_common.bus = bus;
168c566df45SLoGin }
169c566df45SLoGin
set_class(&self, class: Option<Weak<dyn Class>>)1704256da7fSLoGin fn set_class(&self, class: Option<Weak<dyn Class>>) {
171*28fe4ad2S黄铭涛 self.inner().device_common.class = class;
172c566df45SLoGin }
173c566df45SLoGin
class(&self) -> Option<Arc<dyn Class>>1744256da7fSLoGin fn class(&self) -> Option<Arc<dyn Class>> {
175*28fe4ad2S黄铭涛 let mut guard = self.inner();
1764256da7fSLoGin
177*28fe4ad2S黄铭涛 let r = guard.device_common.class.clone()?.upgrade();
1784256da7fSLoGin if r.is_none() {
1794256da7fSLoGin // 为了让弱引用失效
180*28fe4ad2S黄铭涛 guard.device_common.class = None;
1814256da7fSLoGin }
1824256da7fSLoGin
1834256da7fSLoGin return r;
1844256da7fSLoGin }
1854256da7fSLoGin
driver(&self) -> Option<Arc<dyn Driver>>186c566df45SLoGin fn driver(&self) -> Option<Arc<dyn Driver>> {
187*28fe4ad2S黄铭涛 self.inner().device_common.driver.clone()?.upgrade()
188c566df45SLoGin }
189c566df45SLoGin
set_driver(&self, driver: Option<Weak<dyn Driver>>)190c566df45SLoGin fn set_driver(&self, driver: Option<Weak<dyn Driver>>) {
191*28fe4ad2S黄铭涛 self.inner().device_common.driver = driver;
192c566df45SLoGin }
193c566df45SLoGin
is_dead(&self) -> bool194c566df45SLoGin fn is_dead(&self) -> bool {
195c566df45SLoGin false
196c566df45SLoGin }
197c566df45SLoGin
can_match(&self) -> bool198c566df45SLoGin fn can_match(&self) -> bool {
199c566df45SLoGin true
200c566df45SLoGin }
201c566df45SLoGin
set_can_match(&self, _can_match: bool)202c566df45SLoGin fn set_can_match(&self, _can_match: bool) {}
203c566df45SLoGin
state_synced(&self) -> bool204c566df45SLoGin fn state_synced(&self) -> bool {
205c566df45SLoGin true
206c566df45SLoGin }
207*28fe4ad2S黄铭涛
dev_parent(&self) -> Option<Weak<dyn Device>>208*28fe4ad2S黄铭涛 fn dev_parent(&self) -> Option<Weak<dyn Device>> {
209*28fe4ad2S黄铭涛 self.inner().device_common.get_parent_weak_or_clear()
210*28fe4ad2S黄铭涛 }
211*28fe4ad2S黄铭涛
set_dev_parent(&self, dev_parent: Option<Weak<dyn Device>>)212*28fe4ad2S黄铭涛 fn set_dev_parent(&self, dev_parent: Option<Weak<dyn Device>>) {
213*28fe4ad2S黄铭涛 self.inner().device_common.parent = dev_parent;
214*28fe4ad2S黄铭涛 }
215c566df45SLoGin }
216c566df45SLoGin
217c566df45SLoGin impl KObject for VesaFb {
as_any_ref(&self) -> &dyn core::any::Any218c566df45SLoGin fn as_any_ref(&self) -> &dyn core::any::Any {
219c566df45SLoGin self
220c566df45SLoGin }
221c566df45SLoGin
set_inode(&self, inode: Option<Arc<KernFSInode>>)222c566df45SLoGin fn set_inode(&self, inode: Option<Arc<KernFSInode>>) {
223*28fe4ad2S黄铭涛 self.inner().kobject_common.kern_inode = inode;
224c566df45SLoGin }
225c566df45SLoGin
inode(&self) -> Option<Arc<KernFSInode>>226c566df45SLoGin fn inode(&self) -> Option<Arc<KernFSInode>> {
227*28fe4ad2S黄铭涛 self.inner().kobject_common.kern_inode.clone()
228c566df45SLoGin }
229c566df45SLoGin
parent(&self) -> Option<Weak<dyn KObject>>230c566df45SLoGin fn parent(&self) -> Option<Weak<dyn KObject>> {
231*28fe4ad2S黄铭涛 self.inner().kobject_common.parent.clone()
232c566df45SLoGin }
233c566df45SLoGin
set_parent(&self, parent: Option<Weak<dyn KObject>>)234c566df45SLoGin fn set_parent(&self, parent: Option<Weak<dyn KObject>>) {
235*28fe4ad2S黄铭涛 self.inner().kobject_common.parent = parent;
236c566df45SLoGin }
237c566df45SLoGin
kset(&self) -> Option<Arc<KSet>>238c566df45SLoGin fn kset(&self) -> Option<Arc<KSet>> {
239*28fe4ad2S黄铭涛 self.inner().kobject_common.kset.clone()
240c566df45SLoGin }
241c566df45SLoGin
set_kset(&self, kset: Option<Arc<KSet>>)242c566df45SLoGin fn set_kset(&self, kset: Option<Arc<KSet>>) {
243*28fe4ad2S黄铭涛 self.inner().kobject_common.kset = kset;
244c566df45SLoGin }
245c566df45SLoGin
kobj_type(&self) -> Option<&'static dyn KObjType>246c566df45SLoGin fn kobj_type(&self) -> Option<&'static dyn KObjType> {
247*28fe4ad2S黄铭涛 self.inner().kobject_common.kobj_type
248c566df45SLoGin }
249c566df45SLoGin
set_kobj_type(&self, ktype: Option<&'static dyn KObjType>)250c566df45SLoGin fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) {
251*28fe4ad2S黄铭涛 self.inner().kobject_common.kobj_type = ktype;
252c566df45SLoGin }
253c566df45SLoGin
name(&self) -> String254c566df45SLoGin fn name(&self) -> String {
255c566df45SLoGin Self::NAME.to_string()
256c566df45SLoGin }
257c566df45SLoGin
set_name(&self, _name: String)258c566df45SLoGin fn set_name(&self, _name: String) {
259c566df45SLoGin // do nothing
260c566df45SLoGin }
261c566df45SLoGin
kobj_state(&self) -> RwLockReadGuard<KObjectState>262c566df45SLoGin fn kobj_state(&self) -> RwLockReadGuard<KObjectState> {
263c566df45SLoGin self.kobj_state.read()
264c566df45SLoGin }
265c566df45SLoGin
kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState>266c566df45SLoGin fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
267c566df45SLoGin self.kobj_state.write()
268c566df45SLoGin }
269c566df45SLoGin
set_kobj_state(&self, state: KObjectState)270c566df45SLoGin fn set_kobj_state(&self, state: KObjectState) {
271c566df45SLoGin *self.kobj_state.write() = state;
272c566df45SLoGin }
273c566df45SLoGin }
274c566df45SLoGin
275c566df45SLoGin impl FrameBufferOps for VesaFb {
fb_open(&self, _user: bool)276c566df45SLoGin fn fb_open(&self, _user: bool) {
277c566df45SLoGin todo!()
278c566df45SLoGin }
279c566df45SLoGin
fb_release(&self, _user: bool)280c566df45SLoGin fn fb_release(&self, _user: bool) {
281c566df45SLoGin todo!()
282c566df45SLoGin }
283c566df45SLoGin
fb_set_color_register( &self, regno: u16, mut red: u16, mut green: u16, mut blue: u16, ) -> Result<(), SystemError>284c566df45SLoGin fn fb_set_color_register(
285c566df45SLoGin &self,
28652da9a59SGnoCiYeH regno: u16,
28752da9a59SGnoCiYeH mut red: u16,
28852da9a59SGnoCiYeH mut green: u16,
28952da9a59SGnoCiYeH mut blue: u16,
290c566df45SLoGin ) -> Result<(), SystemError> {
29152da9a59SGnoCiYeH let mut fb_data = self.framebuffer_info_data().write();
29252da9a59SGnoCiYeH let var = self.current_fb_var();
29352da9a59SGnoCiYeH if regno as usize >= fb_data.pesudo_palette.len() {
29452da9a59SGnoCiYeH return Err(SystemError::E2BIG);
29552da9a59SGnoCiYeH }
29652da9a59SGnoCiYeH
29752da9a59SGnoCiYeH if var.bits_per_pixel == 8 {
29852da9a59SGnoCiYeH todo!("vesa_setpalette todo");
29952da9a59SGnoCiYeH } else if regno < 16 {
30052da9a59SGnoCiYeH match var.bits_per_pixel {
30152da9a59SGnoCiYeH 16 => {
30252da9a59SGnoCiYeH if var.red.offset == 10 {
30352da9a59SGnoCiYeH // RGB 1:5:5:5
30452da9a59SGnoCiYeH fb_data.pesudo_palette[regno as usize] = ((red as u32 & 0xf800) >> 1)
30552da9a59SGnoCiYeH | ((green as u32 & 0xf800) >> 6)
30652da9a59SGnoCiYeH | ((blue as u32 & 0xf800) >> 11);
30752da9a59SGnoCiYeH } else {
30852da9a59SGnoCiYeH fb_data.pesudo_palette[regno as usize] = (red as u32 & 0xf800)
30952da9a59SGnoCiYeH | ((green as u32 & 0xfc00) >> 5)
31052da9a59SGnoCiYeH | ((blue as u32 & 0xf800) >> 11);
31152da9a59SGnoCiYeH }
31252da9a59SGnoCiYeH }
31352da9a59SGnoCiYeH 24 | 32 => {
31452da9a59SGnoCiYeH red >>= 8;
31552da9a59SGnoCiYeH green >>= 8;
31652da9a59SGnoCiYeH blue >>= 8;
31752da9a59SGnoCiYeH fb_data.pesudo_palette[regno as usize] = ((red as u32) << var.red.offset)
31852da9a59SGnoCiYeH | ((green as u32) << var.green.offset)
31952da9a59SGnoCiYeH | ((blue as u32) << var.blue.offset);
32052da9a59SGnoCiYeH }
32152da9a59SGnoCiYeH _ => {}
32252da9a59SGnoCiYeH }
32352da9a59SGnoCiYeH }
32452da9a59SGnoCiYeH
32552da9a59SGnoCiYeH Ok(())
326c566df45SLoGin }
327c566df45SLoGin
fb_blank(&self, _blank_mode: BlankMode) -> Result<(), SystemError>328c566df45SLoGin fn fb_blank(&self, _blank_mode: BlankMode) -> Result<(), SystemError> {
329c566df45SLoGin todo!()
330c566df45SLoGin }
331c566df45SLoGin
fb_destroy(&self)332c566df45SLoGin fn fb_destroy(&self) {
333c566df45SLoGin todo!()
334c566df45SLoGin }
33502343d0bSLoGin
fb_read(&self, buf: &mut [u8], pos: usize) -> Result<usize, SystemError>33602343d0bSLoGin fn fb_read(&self, buf: &mut [u8], pos: usize) -> Result<usize, SystemError> {
33702343d0bSLoGin let bp = boot_params().read();
33802343d0bSLoGin
33902343d0bSLoGin let vaddr = bp.screen_info.lfb_virt_base.ok_or(SystemError::ENODEV)?;
34002343d0bSLoGin let size = self.current_fb_fix().smem_len;
34102343d0bSLoGin drop(bp);
34202343d0bSLoGin if pos >= size {
34302343d0bSLoGin return Ok(0);
34402343d0bSLoGin }
34502343d0bSLoGin
34602343d0bSLoGin let pos = pos as i64;
34702343d0bSLoGin let size = size as i64;
34802343d0bSLoGin
34902343d0bSLoGin let len = core::cmp::min(size - pos, buf.len() as i64) as usize;
35002343d0bSLoGin
35102343d0bSLoGin let slice = unsafe { core::slice::from_raw_parts(vaddr.as_ptr::<u8>(), size as usize) };
35202343d0bSLoGin buf[..len].copy_from_slice(&slice[pos as usize..(pos as usize + len)]);
35302343d0bSLoGin
35402343d0bSLoGin return Ok(len);
35502343d0bSLoGin }
35602343d0bSLoGin
fb_write(&self, buf: &[u8], pos: usize) -> Result<usize, SystemError>35702343d0bSLoGin fn fb_write(&self, buf: &[u8], pos: usize) -> Result<usize, SystemError> {
35802343d0bSLoGin let bp = boot_params().read();
35902343d0bSLoGin
36002343d0bSLoGin let vaddr = bp.screen_info.lfb_virt_base.ok_or(SystemError::ENODEV)?;
36102343d0bSLoGin let size = self.current_fb_fix().smem_len;
36202343d0bSLoGin
36302343d0bSLoGin if pos >= size {
36402343d0bSLoGin return Ok(0);
36502343d0bSLoGin }
36602343d0bSLoGin
36702343d0bSLoGin let pos = pos as i64;
36802343d0bSLoGin let size = size as i64;
36902343d0bSLoGin
37002343d0bSLoGin let len = core::cmp::min(size - pos, buf.len() as i64) as usize;
37102343d0bSLoGin
37202343d0bSLoGin let slice = unsafe { core::slice::from_raw_parts_mut(vaddr.as_ptr::<u8>(), size as usize) };
37302343d0bSLoGin slice[pos as usize..(pos as usize + len)].copy_from_slice(&buf[..len]);
37402343d0bSLoGin
37502343d0bSLoGin return Ok(len);
37602343d0bSLoGin }
37752da9a59SGnoCiYeH
fb_image_blit(&self, image: &super::base::FbImage)37852da9a59SGnoCiYeH fn fb_image_blit(&self, image: &super::base::FbImage) {
37952da9a59SGnoCiYeH self.generic_imageblit(image);
38052da9a59SGnoCiYeH }
38152da9a59SGnoCiYeH
38252da9a59SGnoCiYeH /// ## 填充矩形
fb_fillrect(&self, rect: super::base::FillRectData) -> Result<(), SystemError>38352da9a59SGnoCiYeH fn fb_fillrect(&self, rect: super::base::FillRectData) -> Result<(), SystemError> {
3842eab6dd7S曾俊 // warn!("rect {rect:?}");
38552da9a59SGnoCiYeH
38652da9a59SGnoCiYeH let boot_param = boot_params().read();
38752da9a59SGnoCiYeH let screen_base = boot_param
38852da9a59SGnoCiYeH .screen_info
38952da9a59SGnoCiYeH .lfb_virt_base
39052da9a59SGnoCiYeH .ok_or(SystemError::ENODEV)?;
391b5b571e0SLoGin
392b5b571e0SLoGin let fg = if self.current_fb_fix().visual == FbVisual::TrueColor
39352da9a59SGnoCiYeH || self.current_fb_fix().visual == FbVisual::DirectColor
39452da9a59SGnoCiYeH {
395b5b571e0SLoGin self.fb_data.read().pesudo_palette[rect.color as usize]
39652da9a59SGnoCiYeH } else {
397b5b571e0SLoGin rect.color
398b5b571e0SLoGin };
39952da9a59SGnoCiYeH
40052da9a59SGnoCiYeH let bpp = self.current_fb_var().bits_per_pixel;
40152da9a59SGnoCiYeH // 每行像素数
40252da9a59SGnoCiYeH let line_offset = self.current_fb_var().xres;
40352da9a59SGnoCiYeH match bpp {
40452da9a59SGnoCiYeH 32 => {
40552da9a59SGnoCiYeH let base = screen_base.as_ptr::<u32>();
40652da9a59SGnoCiYeH
40752da9a59SGnoCiYeH for y in rect.dy..(rect.dy + rect.height) {
40852da9a59SGnoCiYeH for x in rect.dx..(rect.dx + rect.width) {
40952da9a59SGnoCiYeH unsafe { *base.add((y * line_offset + x) as usize) = fg };
41052da9a59SGnoCiYeH }
41152da9a59SGnoCiYeH }
41252da9a59SGnoCiYeH }
4132755467cS曾俊 16 => {
4142755467cS曾俊 let base = screen_base.as_ptr::<u16>();
4152755467cS曾俊
4162755467cS曾俊 for y in rect.dy..(rect.dy + rect.height) {
4172755467cS曾俊 for x in rect.dx..(rect.dx + rect.width) {
4182755467cS曾俊 unsafe { *base.add((y * line_offset + x) as usize) = 0x0000 };
4192755467cS曾俊 }
4202755467cS曾俊 }
4212755467cS曾俊 }
4222755467cS曾俊 24 => {
4232755467cS曾俊 let base = screen_base.as_ptr::<[u8; 3]>();
4242755467cS曾俊
4252755467cS曾俊 for y in rect.dy..(rect.dy + rect.height) {
4262755467cS曾俊 for x in rect.dx..(rect.dx + rect.width) {
4272755467cS曾俊 unsafe { *base.add((y * line_offset + x) as usize) = [0, 0, 0] };
4282755467cS曾俊 }
4292755467cS曾俊 }
4302755467cS曾俊 }
4312755467cS曾俊 _ => {
4322755467cS曾俊 send_to_default_serial8250_port(
4332755467cS曾俊 format!("unsupported bit depth:{}!\n\0", bpp).as_bytes(),
4342755467cS曾俊 );
4352755467cS曾俊 todo!()
4362755467cS曾俊 }
43752da9a59SGnoCiYeH }
43852da9a59SGnoCiYeH
43952da9a59SGnoCiYeH Ok(())
44052da9a59SGnoCiYeH }
44152da9a59SGnoCiYeH
44252bcb59eSGnoCiYeH #[inline(never)]
fb_copyarea(&self, data: super::base::CopyAreaData)44352bcb59eSGnoCiYeH fn fb_copyarea(&self, data: super::base::CopyAreaData) {
44452da9a59SGnoCiYeH let bp = boot_params().read();
44552bcb59eSGnoCiYeH let base = bp.screen_info.lfb_virt_base.unwrap();
44652da9a59SGnoCiYeH let var = self.current_fb_var();
44752da9a59SGnoCiYeH
44852bcb59eSGnoCiYeH // 原区域或者目标区域全在屏幕外,则直接返回
44952bcb59eSGnoCiYeH if data.sx > var.xres as i32
45052bcb59eSGnoCiYeH || data.sy > var.yres as i32
45152bcb59eSGnoCiYeH || data.dx > var.xres as i32
45252bcb59eSGnoCiYeH || data.dy > var.yres as i32
45352bcb59eSGnoCiYeH || (data.sx + data.width as i32) < 0
45452bcb59eSGnoCiYeH || (data.sy + data.height as i32) < 0
45552bcb59eSGnoCiYeH || (data.dx + data.width as i32) < 0
45652bcb59eSGnoCiYeH || (data.dy + data.height as i32) < 0
45752da9a59SGnoCiYeH {
45852bcb59eSGnoCiYeH return;
45952da9a59SGnoCiYeH }
46052da9a59SGnoCiYeH
46152bcb59eSGnoCiYeH // 求两个矩形可视范围交集
46252bcb59eSGnoCiYeH let (s_visiable_x, s_w) = if data.sx < 0 {
46352bcb59eSGnoCiYeH (0, (data.width - ((-data.sx) as u32)).min(var.xres))
46452bcb59eSGnoCiYeH } else {
46552bcb59eSGnoCiYeH let w = if data.sx as u32 + data.width > var.xres {
46652bcb59eSGnoCiYeH var.xres - data.sx as u32
46752bcb59eSGnoCiYeH } else {
46852bcb59eSGnoCiYeH data.width
46952bcb59eSGnoCiYeH };
47052bcb59eSGnoCiYeH
47152bcb59eSGnoCiYeH (data.sx, w)
47252bcb59eSGnoCiYeH };
47352bcb59eSGnoCiYeH let (s_visiable_y, s_h) = if data.sy < 0 {
47452bcb59eSGnoCiYeH (0, (data.height - ((-data.sy) as u32).min(var.yres)))
47552bcb59eSGnoCiYeH } else {
47652bcb59eSGnoCiYeH let h = if data.sy as u32 + data.height > var.yres {
47752bcb59eSGnoCiYeH var.yres - data.sy as u32
47852bcb59eSGnoCiYeH } else {
47952bcb59eSGnoCiYeH data.height
48052bcb59eSGnoCiYeH };
48152bcb59eSGnoCiYeH
48252bcb59eSGnoCiYeH (data.sy, h)
48352bcb59eSGnoCiYeH };
48452bcb59eSGnoCiYeH
48552bcb59eSGnoCiYeH let (d_visiable_x, d_w) = if data.dx < 0 {
48652bcb59eSGnoCiYeH (0, (data.width - ((-data.dx) as u32)).min(var.xres))
48752bcb59eSGnoCiYeH } else {
48852bcb59eSGnoCiYeH let w = if data.dx as u32 + data.width > var.xres {
48952bcb59eSGnoCiYeH var.xres - data.dx as u32
49052bcb59eSGnoCiYeH } else {
49152bcb59eSGnoCiYeH data.width
49252bcb59eSGnoCiYeH };
49352bcb59eSGnoCiYeH
49452bcb59eSGnoCiYeH (data.dx, w)
49552bcb59eSGnoCiYeH };
49652bcb59eSGnoCiYeH let (d_visiable_y, d_h) = if data.dy < 0 {
49752bcb59eSGnoCiYeH (0, (data.height - ((-data.dy) as u32).min(var.yres)))
49852bcb59eSGnoCiYeH } else {
49952bcb59eSGnoCiYeH let h = if data.dy as u32 + data.height > var.yres {
50052bcb59eSGnoCiYeH var.yres - data.dy as u32
50152bcb59eSGnoCiYeH } else {
50252bcb59eSGnoCiYeH data.height
50352bcb59eSGnoCiYeH };
50452bcb59eSGnoCiYeH
50552bcb59eSGnoCiYeH (data.dy, h)
50652bcb59eSGnoCiYeH };
50752bcb59eSGnoCiYeH
50852bcb59eSGnoCiYeH // 可视范围无交集
50952bcb59eSGnoCiYeH if !(d_h + s_h > data.height && s_w + d_w > data.width) {
51052bcb59eSGnoCiYeH return;
51152bcb59eSGnoCiYeH }
51252bcb59eSGnoCiYeH
51352bcb59eSGnoCiYeH // 可视区域左上角相对于矩形的坐标
51452bcb59eSGnoCiYeH let s_relative_x = s_visiable_x - data.sx;
51552bcb59eSGnoCiYeH let s_relative_y = s_visiable_y - data.sy;
51652bcb59eSGnoCiYeH let d_relative_x = d_visiable_x - data.dx;
51752bcb59eSGnoCiYeH let d_relative_y = d_visiable_y - data.dy;
51852bcb59eSGnoCiYeH
51952bcb59eSGnoCiYeH let visiable_x = s_relative_x.max(d_relative_x);
52052bcb59eSGnoCiYeH let visiable_y = s_relative_y.max(d_relative_y);
52152bcb59eSGnoCiYeH let visiable_h = d_h + s_h - data.height;
52252bcb59eSGnoCiYeH let visiable_w = d_w + s_w - data.width;
52352bcb59eSGnoCiYeH
52452bcb59eSGnoCiYeH let s_real_x = (visiable_x + data.sx) as u32;
52552bcb59eSGnoCiYeH let s_real_y = (visiable_y + data.sy) as u32;
52652bcb59eSGnoCiYeH let d_real_x = (visiable_x + data.dx) as u32;
52752bcb59eSGnoCiYeH let d_real_y = (visiable_y + data.dy) as u32;
52852bcb59eSGnoCiYeH
52952da9a59SGnoCiYeH let bytes_per_pixel = var.bits_per_pixel >> 3;
53052da9a59SGnoCiYeH let bytes_per_line = var.xres * bytes_per_pixel;
53152da9a59SGnoCiYeH
53252bcb59eSGnoCiYeH let src =
53352bcb59eSGnoCiYeH base + VirtAddr::new((s_real_y * bytes_per_line + s_real_x * bytes_per_pixel) as usize);
53452da9a59SGnoCiYeH
53552bcb59eSGnoCiYeH let dst =
53652bcb59eSGnoCiYeH base + VirtAddr::new((d_real_y * bytes_per_line + d_real_x * bytes_per_pixel) as usize);
53752da9a59SGnoCiYeH
53852bcb59eSGnoCiYeH let size = (visiable_h * visiable_w) as usize;
53952da9a59SGnoCiYeH
54052da9a59SGnoCiYeH match bytes_per_pixel {
54152da9a59SGnoCiYeH 4 => {
54252da9a59SGnoCiYeH // 32bpp
54352da9a59SGnoCiYeH let mut dst = dst.as_ptr::<u32>();
54452da9a59SGnoCiYeH let mut src = src.as_ptr::<u32>();
54552bcb59eSGnoCiYeH let line_offset = var.xres as usize;
54652da9a59SGnoCiYeH
54752bcb59eSGnoCiYeH if s_real_x > d_real_x {
54852bcb59eSGnoCiYeH // 如果src在dst下方,则可以直接拷贝不会出现指针覆盖
54952da9a59SGnoCiYeH unsafe {
55052bcb59eSGnoCiYeH for _ in 0..visiable_h {
55152bcb59eSGnoCiYeH core::ptr::copy(src, dst, visiable_w as usize);
55252bcb59eSGnoCiYeH src = src.add(line_offset);
55352bcb59eSGnoCiYeH dst = dst.add(visiable_w as usize);
55452da9a59SGnoCiYeH }
55552da9a59SGnoCiYeH }
55652da9a59SGnoCiYeH } else {
557b5b571e0SLoGin let mut tmp: Vec<u32> = vec![0; size];
55852bcb59eSGnoCiYeH let mut tmp_ptr = tmp.as_mut_ptr();
55952bcb59eSGnoCiYeH
56052bcb59eSGnoCiYeH // 这里是一个可以优化的点,现在为了避免指针拷贝时覆盖,统一先拷贝进入buf再拷贝到dst
56152da9a59SGnoCiYeH unsafe {
56252bcb59eSGnoCiYeH for _ in 0..visiable_h {
56352bcb59eSGnoCiYeH core::ptr::copy(src, tmp_ptr, visiable_w as usize);
56452bcb59eSGnoCiYeH src = src.add(line_offset);
56552bcb59eSGnoCiYeH tmp_ptr = tmp_ptr.add(visiable_w as usize);
56652bcb59eSGnoCiYeH }
56752bcb59eSGnoCiYeH
56852bcb59eSGnoCiYeH tmp_ptr = tmp_ptr.sub(size);
56952bcb59eSGnoCiYeH for _ in 0..visiable_h {
57052bcb59eSGnoCiYeH core::ptr::copy(tmp_ptr, dst, visiable_w as usize);
57152bcb59eSGnoCiYeH dst = dst.add(line_offset);
57252bcb59eSGnoCiYeH tmp_ptr = tmp_ptr.add(visiable_w as usize);
57352da9a59SGnoCiYeH }
57452da9a59SGnoCiYeH }
57552da9a59SGnoCiYeH }
57652da9a59SGnoCiYeH }
5772755467cS曾俊 2 => {
5782755467cS曾俊 let mut dst = dst.as_ptr::<u16>();
5792755467cS曾俊 let mut src = src.as_ptr::<u16>();
5802755467cS曾俊 let line_offset = var.xres as usize;
5812755467cS曾俊
5822755467cS曾俊 if s_real_x > d_real_x {
5832755467cS曾俊 // 如果src在dst下方,则可以直接拷贝不会出现指针覆盖
5842755467cS曾俊 unsafe {
5852755467cS曾俊 for _ in 0..visiable_h {
5862755467cS曾俊 core::ptr::copy(src, dst, visiable_w as usize);
5872755467cS曾俊 src = src.add(line_offset);
5882755467cS曾俊 dst = dst.add(visiable_w as usize);
5892755467cS曾俊 }
5902755467cS曾俊 }
5912755467cS曾俊 } else {
5922755467cS曾俊 let mut tmp: Vec<u16> = vec![0; size];
5932755467cS曾俊 let mut tmp_ptr = tmp.as_mut_ptr();
5942755467cS曾俊
5952755467cS曾俊 // 这里是一个可以优化的点,现在为了避免指针拷贝时覆盖,统一先拷贝进入buf再拷贝到dst
5962755467cS曾俊 unsafe {
5972755467cS曾俊 for _ in 0..visiable_h {
5982755467cS曾俊 core::ptr::copy(src, tmp_ptr, visiable_w as usize);
5992755467cS曾俊 src = src.add(line_offset);
6002755467cS曾俊 tmp_ptr = tmp_ptr.add(visiable_w as usize);
6012755467cS曾俊 }
6022755467cS曾俊
6032755467cS曾俊 tmp_ptr = tmp_ptr.sub(size);
6042755467cS曾俊 for _ in 0..visiable_h {
6052755467cS曾俊 core::ptr::copy(tmp_ptr, dst, visiable_w as usize);
6062755467cS曾俊 dst = dst.add(line_offset);
6072755467cS曾俊 tmp_ptr = tmp_ptr.add(visiable_w as usize);
6082755467cS曾俊 }
6092755467cS曾俊 }
6102755467cS曾俊 }
6112755467cS曾俊 }
6122755467cS曾俊 3 => {
6132755467cS曾俊 let mut dst = dst.as_ptr::<[u8; 3]>();
6142755467cS曾俊 let mut src = src.as_ptr::<[u8; 3]>();
6152755467cS曾俊 let line_offset = var.xres as usize;
6162755467cS曾俊
6172755467cS曾俊 if s_real_x > d_real_x {
6182755467cS曾俊 // 如果src在dst下方,则可以直接拷贝不会出现指针覆盖
6192755467cS曾俊 unsafe {
6202755467cS曾俊 for _ in 0..visiable_h {
6212755467cS曾俊 core::ptr::copy(src, dst, visiable_w as usize);
6222755467cS曾俊 src = src.add(line_offset);
6232755467cS曾俊 dst = dst.add(visiable_w as usize);
6242755467cS曾俊 }
6252755467cS曾俊 }
6262755467cS曾俊 } else {
6272755467cS曾俊 let mut tmp: Vec<u32> = vec![0; size];
6282755467cS曾俊 let mut tmp_ptr = tmp.as_mut_ptr() as *mut [u8; 3];
6292755467cS曾俊
6302755467cS曾俊 // 这里是一个可以优化的点,现在为了避免指针拷贝时覆盖,统一先拷贝进入buf再拷贝到dst
6312755467cS曾俊 unsafe {
6322755467cS曾俊 for _ in 0..visiable_h {
6332755467cS曾俊 core::ptr::copy(src, tmp_ptr, visiable_w as usize);
6342755467cS曾俊 src = src.add(line_offset);
6352755467cS曾俊 tmp_ptr = tmp_ptr.add(visiable_w as usize);
6362755467cS曾俊 }
6372755467cS曾俊
6382755467cS曾俊 tmp_ptr = tmp_ptr.sub(size);
6392755467cS曾俊 for _ in 0..visiable_h {
6402755467cS曾俊 core::ptr::copy(tmp_ptr, dst, visiable_w as usize);
6412755467cS曾俊 dst = dst.add(line_offset);
6422755467cS曾俊 tmp_ptr = tmp_ptr.add(visiable_w as usize);
6432755467cS曾俊 }
6442755467cS曾俊 }
6452755467cS曾俊 }
6462755467cS曾俊 }
6472755467cS曾俊
64852da9a59SGnoCiYeH _ => {
6492755467cS曾俊 send_to_default_serial8250_port(
6502755467cS曾俊 format!("bytes_per_pixel:{}\n\0", bytes_per_pixel).as_bytes(),
6512755467cS曾俊 );
65252da9a59SGnoCiYeH todo!()
65352da9a59SGnoCiYeH }
65452da9a59SGnoCiYeH }
65552da9a59SGnoCiYeH }
656c566df45SLoGin }
657c566df45SLoGin
658c566df45SLoGin impl FrameBufferInfo for VesaFb {
fb_device(&self) -> Option<Arc<FbDevice>>659c566df45SLoGin fn fb_device(&self) -> Option<Arc<FbDevice>> {
660c566df45SLoGin self.inner.lock().fb_device.clone()
661c566df45SLoGin }
662c566df45SLoGin
set_fb_device(&self, device: Option<Arc<FbDevice>>)663c566df45SLoGin fn set_fb_device(&self, device: Option<Arc<FbDevice>>) {
664c566df45SLoGin self.inner.lock().fb_device = device;
665c566df45SLoGin }
666c566df45SLoGin
screen_size(&self) -> usize667c566df45SLoGin fn screen_size(&self) -> usize {
668c566df45SLoGin todo!()
669c566df45SLoGin }
670c566df45SLoGin
current_fb_var(&self) -> FbVarScreenInfo671c566df45SLoGin fn current_fb_var(&self) -> FbVarScreenInfo {
672b5b571e0SLoGin *VESAFB_DEFINED.read()
673c566df45SLoGin }
674c566df45SLoGin
current_fb_fix(&self) -> FixedScreenInfo675c566df45SLoGin fn current_fb_fix(&self) -> FixedScreenInfo {
676b5b571e0SLoGin *VESAFB_FIX_INFO.read()
677c566df45SLoGin }
678c566df45SLoGin
video_mode(&self) -> Option<&FbVideoMode>679c566df45SLoGin fn video_mode(&self) -> Option<&FbVideoMode> {
680c566df45SLoGin todo!()
681c566df45SLoGin }
682c566df45SLoGin
state(&self) -> FbState683c566df45SLoGin fn state(&self) -> FbState {
684c566df45SLoGin self.inner.lock().fb_state
685c566df45SLoGin }
68652da9a59SGnoCiYeH
framebuffer_info_data(&self) -> &RwLock<FrameBufferInfoData>68752da9a59SGnoCiYeH fn framebuffer_info_data(&self) -> &RwLock<FrameBufferInfoData> {
68852da9a59SGnoCiYeH &self.fb_data
68952da9a59SGnoCiYeH }
690c566df45SLoGin }
691c566df45SLoGin
692c566df45SLoGin #[derive(Debug)]
693c566df45SLoGin #[cast_to([sync] PlatformDriver)]
694c566df45SLoGin struct VesaFbDriver {
695c566df45SLoGin inner: SpinLock<InnerVesaFbDriver>,
696c566df45SLoGin kobj_state: LockedKObjectState,
697c566df45SLoGin }
698c566df45SLoGin
699c566df45SLoGin impl VesaFbDriver {
new() -> Arc<Self>700c566df45SLoGin pub fn new() -> Arc<Self> {
701c566df45SLoGin let r = Arc::new(Self {
702c566df45SLoGin inner: SpinLock::new(InnerVesaFbDriver {
703c566df45SLoGin ktype: None,
704c566df45SLoGin kset: None,
705c566df45SLoGin parent: None,
706c566df45SLoGin kernfs_inode: None,
707c566df45SLoGin devices: Vec::new(),
708c566df45SLoGin bus: None,
709c566df45SLoGin self_ref: Weak::new(),
710c566df45SLoGin }),
711c566df45SLoGin kobj_state: LockedKObjectState::new(None),
712c566df45SLoGin });
713c566df45SLoGin
714c566df45SLoGin r.inner.lock().self_ref = Arc::downgrade(&r);
715c566df45SLoGin
716c566df45SLoGin return r;
717c566df45SLoGin }
718c566df45SLoGin }
719c566df45SLoGin
720c566df45SLoGin #[derive(Debug)]
721c566df45SLoGin struct InnerVesaFbDriver {
722c566df45SLoGin ktype: Option<&'static dyn KObjType>,
723c566df45SLoGin kset: Option<Arc<KSet>>,
724c566df45SLoGin parent: Option<Weak<dyn KObject>>,
725c566df45SLoGin kernfs_inode: Option<Arc<KernFSInode>>,
726c566df45SLoGin devices: Vec<Arc<dyn Device>>,
727c566df45SLoGin bus: Option<Weak<dyn Bus>>,
728c566df45SLoGin
729c566df45SLoGin self_ref: Weak<VesaFbDriver>,
730c566df45SLoGin }
731c566df45SLoGin
732c566df45SLoGin impl VesaFbDriver {
733c566df45SLoGin const NAME: &'static str = "vesa-framebuffer";
734c566df45SLoGin }
735c566df45SLoGin
736c566df45SLoGin impl PlatformDriver for VesaFbDriver {
probe(&self, device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError>737c566df45SLoGin fn probe(&self, device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> {
738c566df45SLoGin let device = device
739c566df45SLoGin .clone()
740c566df45SLoGin .arc_any()
741c566df45SLoGin .downcast::<VesaFb>()
742c566df45SLoGin .map_err(|_| SystemError::EINVAL)?;
743c566df45SLoGin
744c566df45SLoGin device.set_driver(Some(self.inner.lock_irqsave().self_ref.clone()));
745c566df45SLoGin
746c566df45SLoGin return Ok(());
747c566df45SLoGin }
748c566df45SLoGin
remove(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError>749c566df45SLoGin fn remove(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> {
750c566df45SLoGin todo!()
751c566df45SLoGin }
752c566df45SLoGin
shutdown(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError>753c566df45SLoGin fn shutdown(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> {
754c566df45SLoGin // do nothing
755c566df45SLoGin return Ok(());
756c566df45SLoGin }
757c566df45SLoGin
suspend(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError>758c566df45SLoGin fn suspend(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> {
759c566df45SLoGin // do nothing
760c566df45SLoGin return Ok(());
761c566df45SLoGin }
762c566df45SLoGin
resume(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError>763c566df45SLoGin fn resume(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> {
764c566df45SLoGin todo!()
765c566df45SLoGin }
766c566df45SLoGin }
767c566df45SLoGin
768c566df45SLoGin impl Driver for VesaFbDriver {
id_table(&self) -> Option<IdTable>769c566df45SLoGin fn id_table(&self) -> Option<IdTable> {
770c566df45SLoGin Some(IdTable::new(VesaFb::NAME.to_string(), None))
771c566df45SLoGin }
772c566df45SLoGin
devices(&self) -> Vec<Arc<dyn Device>>773c566df45SLoGin fn devices(&self) -> Vec<Arc<dyn Device>> {
774c566df45SLoGin self.inner.lock().devices.clone()
775c566df45SLoGin }
776c566df45SLoGin
add_device(&self, device: Arc<dyn Device>)777c566df45SLoGin fn add_device(&self, device: Arc<dyn Device>) {
778c566df45SLoGin let mut guard = self.inner.lock();
779c566df45SLoGin // check if the device is already in the list
780c566df45SLoGin if guard.devices.iter().any(|dev| Arc::ptr_eq(dev, &device)) {
781c566df45SLoGin return;
782c566df45SLoGin }
783c566df45SLoGin
784c566df45SLoGin guard.devices.push(device);
785c566df45SLoGin }
786c566df45SLoGin
delete_device(&self, device: &Arc<dyn Device>)787c566df45SLoGin fn delete_device(&self, device: &Arc<dyn Device>) {
788c566df45SLoGin let mut guard = self.inner.lock();
789c566df45SLoGin guard.devices.retain(|dev| !Arc::ptr_eq(dev, device));
790c566df45SLoGin }
791c566df45SLoGin
set_bus(&self, bus: Option<Weak<dyn Bus>>)792c566df45SLoGin fn set_bus(&self, bus: Option<Weak<dyn Bus>>) {
793c566df45SLoGin self.inner.lock().bus = bus;
794c566df45SLoGin }
795c566df45SLoGin
bus(&self) -> Option<Weak<dyn Bus>>796c566df45SLoGin fn bus(&self) -> Option<Weak<dyn Bus>> {
797c566df45SLoGin self.inner.lock().bus.clone()
798c566df45SLoGin }
799c566df45SLoGin
dev_groups(&self) -> &'static [&'static dyn AttributeGroup]800c566df45SLoGin fn dev_groups(&self) -> &'static [&'static dyn AttributeGroup] {
801c566df45SLoGin return &[&VesaFbAnonAttributeGroup];
802c566df45SLoGin }
803c566df45SLoGin }
804c566df45SLoGin
805c566df45SLoGin impl KObject for VesaFbDriver {
as_any_ref(&self) -> &dyn core::any::Any806c566df45SLoGin fn as_any_ref(&self) -> &dyn core::any::Any {
807c566df45SLoGin self
808c566df45SLoGin }
809c566df45SLoGin
set_inode(&self, inode: Option<Arc<KernFSInode>>)810c566df45SLoGin fn set_inode(&self, inode: Option<Arc<KernFSInode>>) {
811c566df45SLoGin self.inner.lock().kernfs_inode = inode;
812c566df45SLoGin }
813c566df45SLoGin
inode(&self) -> Option<Arc<KernFSInode>>814c566df45SLoGin fn inode(&self) -> Option<Arc<KernFSInode>> {
815c566df45SLoGin self.inner.lock().kernfs_inode.clone()
816c566df45SLoGin }
817c566df45SLoGin
parent(&self) -> Option<Weak<dyn KObject>>818c566df45SLoGin fn parent(&self) -> Option<Weak<dyn KObject>> {
819c566df45SLoGin self.inner.lock().parent.clone()
820c566df45SLoGin }
821c566df45SLoGin
set_parent(&self, parent: Option<Weak<dyn KObject>>)822c566df45SLoGin fn set_parent(&self, parent: Option<Weak<dyn KObject>>) {
823c566df45SLoGin self.inner.lock().parent = parent;
824c566df45SLoGin }
825c566df45SLoGin
kset(&self) -> Option<Arc<KSet>>826c566df45SLoGin fn kset(&self) -> Option<Arc<KSet>> {
827c566df45SLoGin self.inner.lock().kset.clone()
828c566df45SLoGin }
829c566df45SLoGin
set_kset(&self, kset: Option<Arc<KSet>>)830c566df45SLoGin fn set_kset(&self, kset: Option<Arc<KSet>>) {
831c566df45SLoGin self.inner.lock().kset = kset;
832c566df45SLoGin }
833c566df45SLoGin
kobj_type(&self) -> Option<&'static dyn KObjType>834c566df45SLoGin fn kobj_type(&self) -> Option<&'static dyn KObjType> {
835c566df45SLoGin self.inner.lock().ktype
836c566df45SLoGin }
837c566df45SLoGin
set_kobj_type(&self, ktype: Option<&'static dyn KObjType>)838c566df45SLoGin fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) {
839c566df45SLoGin self.inner.lock().ktype = ktype;
840c566df45SLoGin }
841c566df45SLoGin
name(&self) -> String842c566df45SLoGin fn name(&self) -> String {
843c566df45SLoGin Self::NAME.to_string()
844c566df45SLoGin }
845c566df45SLoGin
set_name(&self, _name: String)846c566df45SLoGin fn set_name(&self, _name: String) {
847c566df45SLoGin // do nothing
848c566df45SLoGin }
849c566df45SLoGin
kobj_state(&self) -> RwLockReadGuard<KObjectState>850c566df45SLoGin fn kobj_state(&self) -> RwLockReadGuard<KObjectState> {
851c566df45SLoGin self.kobj_state.read()
852c566df45SLoGin }
853c566df45SLoGin
kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState>854c566df45SLoGin fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
855c566df45SLoGin self.kobj_state.write()
856c566df45SLoGin }
857c566df45SLoGin
set_kobj_state(&self, state: KObjectState)858c566df45SLoGin fn set_kobj_state(&self, state: KObjectState) {
859c566df45SLoGin *self.kobj_state.write() = state;
860c566df45SLoGin }
861c566df45SLoGin }
862c566df45SLoGin
863c566df45SLoGin #[derive(Debug)]
864c566df45SLoGin struct VesaFbAnonAttributeGroup;
865c566df45SLoGin
866c566df45SLoGin impl AttributeGroup for VesaFbAnonAttributeGroup {
name(&self) -> Option<&str>867c566df45SLoGin fn name(&self) -> Option<&str> {
868c566df45SLoGin None
869c566df45SLoGin }
870c566df45SLoGin
attrs(&self) -> &[&'static dyn Attribute]871c566df45SLoGin fn attrs(&self) -> &[&'static dyn Attribute] {
872c566df45SLoGin &[&AnonAttrPhysAddr as &'static dyn Attribute]
873c566df45SLoGin }
874c566df45SLoGin
is_visible( &self, _kobj: Arc<dyn KObject>, attr: &'static dyn Attribute, ) -> Option<ModeType>875c566df45SLoGin fn is_visible(
876c566df45SLoGin &self,
877c566df45SLoGin _kobj: Arc<dyn KObject>,
878c566df45SLoGin attr: &'static dyn Attribute,
879c566df45SLoGin ) -> Option<ModeType> {
880c566df45SLoGin Some(attr.mode())
881c566df45SLoGin }
882c566df45SLoGin }
883c566df45SLoGin
884c566df45SLoGin #[derive(Debug)]
885c566df45SLoGin struct AnonAttrPhysAddr;
886c566df45SLoGin
887c566df45SLoGin impl Attribute for AnonAttrPhysAddr {
name(&self) -> &str888c566df45SLoGin fn name(&self) -> &str {
889c566df45SLoGin "smem_start"
890c566df45SLoGin }
891c566df45SLoGin
mode(&self) -> ModeType892c566df45SLoGin fn mode(&self) -> ModeType {
893c566df45SLoGin ModeType::S_IRUGO
894c566df45SLoGin }
895c566df45SLoGin
support(&self) -> SysFSOpsSupport896c566df45SLoGin fn support(&self) -> SysFSOpsSupport {
897196b75dcSLoGin SysFSOpsSupport::ATTR_SHOW
898c566df45SLoGin }
899c566df45SLoGin
show(&self, _kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError>900c566df45SLoGin fn show(&self, _kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError> {
901c566df45SLoGin sysfs_emit_str(
902c566df45SLoGin buf,
903c566df45SLoGin format!(
904c566df45SLoGin "0x{:x}\n",
905c566df45SLoGin VESAFB_FIX_INFO
906c566df45SLoGin .read()
907c566df45SLoGin .smem_start
908c566df45SLoGin .unwrap_or(PhysAddr::new(0))
909c566df45SLoGin .data()
910c566df45SLoGin )
911c566df45SLoGin .as_str(),
912c566df45SLoGin )
913c566df45SLoGin }
914c566df45SLoGin }
915c566df45SLoGin
916c566df45SLoGin #[unified_init(INITCALL_DEVICE)]
vesa_fb_driver_init() -> Result<(), SystemError>917c566df45SLoGin pub fn vesa_fb_driver_init() -> Result<(), SystemError> {
918c566df45SLoGin let driver = VesaFbDriver::new();
919c566df45SLoGin
920c566df45SLoGin platform_driver_manager().register(driver)?;
921c566df45SLoGin
922c566df45SLoGin return Ok(());
923c566df45SLoGin }
924c566df45SLoGin
925c566df45SLoGin /// 在内存管理初始化之前,初始化vesafb
vesafb_early_init() -> Result<(), SystemError>9262b7818e8SLoGin pub fn vesafb_early_init() -> Result<(), SystemError> {
9272b7818e8SLoGin let mut boot_params_guard = boot_params().write();
9282b7818e8SLoGin boot_callbacks().early_init_framebuffer_info(&mut boot_params_guard.screen_info)?;
929c566df45SLoGin
930c566df45SLoGin HAS_VESA_FB.store(true, core::sync::atomic::Ordering::SeqCst);
931c566df45SLoGin
9322b7818e8SLoGin return Ok(());
933c566df45SLoGin }
934c566df45SLoGin
vesafb_early_map(paddr: PhysAddr, size: usize) -> Result<VirtAddr, SystemError>9352b7818e8SLoGin pub fn vesafb_early_map(paddr: PhysAddr, size: usize) -> Result<VirtAddr, SystemError> {
9362b7818e8SLoGin let (buf_vaddr, _) = EarlyIoRemap::map(paddr, size, false)?;
937c566df45SLoGin
938c566df45SLoGin return Ok(buf_vaddr);
939c566df45SLoGin }
940c566df45SLoGin
941c566df45SLoGin #[unified_init(INITCALL_DEVICE)]
vesa_fb_device_init() -> Result<(), SystemError>942c566df45SLoGin fn vesa_fb_device_init() -> Result<(), SystemError> {
943c566df45SLoGin // 如果没有vesa帧缓冲区,直接返回
944c566df45SLoGin if !HAS_VESA_FB.load(core::sync::atomic::Ordering::SeqCst) {
945c566df45SLoGin return Ok(());
946c566df45SLoGin }
947c566df45SLoGin
948c566df45SLoGin static INIT: Once = Once::new();
949c566df45SLoGin INIT.call_once(|| {
9502eab6dd7S曾俊 info!("vesa fb device init");
95152da9a59SGnoCiYeH let device = Arc::new(VesaFb::new());
952c566df45SLoGin
95352da9a59SGnoCiYeH let mut fb_fix = VESAFB_FIX_INFO.write_irqsave();
95452da9a59SGnoCiYeH let mut fb_var = VESAFB_DEFINED.write_irqsave();
955c566df45SLoGin
956c566df45SLoGin let boot_params_guard = boot_params().read();
957c566df45SLoGin let boottime_screen_info = &boot_params_guard.screen_info;
958c566df45SLoGin
95952da9a59SGnoCiYeH fb_fix.smem_start = Some(boottime_screen_info.lfb_base);
96052da9a59SGnoCiYeH fb_fix.smem_len = boottime_screen_info.lfb_size;
961c566df45SLoGin
962c566df45SLoGin if boottime_screen_info.video_type == BootTimeVideoType::Mda {
96352da9a59SGnoCiYeH fb_fix.visual = FbVisual::Mono10;
96452da9a59SGnoCiYeH fb_var.bits_per_pixel = 8;
96552da9a59SGnoCiYeH fb_fix.line_length =
96652da9a59SGnoCiYeH (boottime_screen_info.origin_video_cols as u32) * (fb_var.bits_per_pixel / 8);
96752da9a59SGnoCiYeH fb_var.xres_virtual = boottime_screen_info.origin_video_cols as u32;
96852da9a59SGnoCiYeH fb_var.yres_virtual = boottime_screen_info.origin_video_lines as u32;
969c566df45SLoGin } else {
97052da9a59SGnoCiYeH fb_fix.visual = FbVisual::TrueColor;
97152da9a59SGnoCiYeH fb_var.bits_per_pixel = boottime_screen_info.lfb_depth as u32;
97252da9a59SGnoCiYeH fb_fix.line_length =
97352da9a59SGnoCiYeH (boottime_screen_info.lfb_width as u32) * (fb_var.bits_per_pixel / 8);
97452da9a59SGnoCiYeH fb_var.xres_virtual = boottime_screen_info.lfb_width as u32;
97552da9a59SGnoCiYeH fb_var.yres_virtual = boottime_screen_info.lfb_height as u32;
97652da9a59SGnoCiYeH fb_var.xres = boottime_screen_info.lfb_width as u32;
97752da9a59SGnoCiYeH fb_var.yres = boottime_screen_info.lfb_height as u32;
978c566df45SLoGin }
979c566df45SLoGin
98052da9a59SGnoCiYeH fb_var.red.length = boottime_screen_info.red_size as u32;
98152da9a59SGnoCiYeH fb_var.green.length = boottime_screen_info.green_size as u32;
98252da9a59SGnoCiYeH fb_var.blue.length = boottime_screen_info.blue_size as u32;
983c566df45SLoGin
98452da9a59SGnoCiYeH fb_var.red.offset = boottime_screen_info.red_pos as u32;
98552da9a59SGnoCiYeH fb_var.green.offset = boottime_screen_info.green_pos as u32;
98652da9a59SGnoCiYeH fb_var.blue.offset = boottime_screen_info.blue_pos as u32;
98752da9a59SGnoCiYeH
98852da9a59SGnoCiYeH // TODO: 这里是暂时这样写的,初始化为RGB888格式,后续vesa初始化完善后删掉下面
98952da9a59SGnoCiYeH fb_var.red.offset = 16;
99052da9a59SGnoCiYeH fb_var.green.offset = 8;
99152da9a59SGnoCiYeH fb_var.blue.offset = 0;
99252da9a59SGnoCiYeH
99352da9a59SGnoCiYeH if fb_var.bits_per_pixel >= 1 && fb_var.bits_per_pixel <= 8 {
99452da9a59SGnoCiYeH fb_var.red.length = fb_var.bits_per_pixel;
99552da9a59SGnoCiYeH fb_var.green.length = fb_var.bits_per_pixel;
99652da9a59SGnoCiYeH fb_var.blue.length = fb_var.bits_per_pixel;
99752da9a59SGnoCiYeH }
99852da9a59SGnoCiYeH
999c566df45SLoGin device_manager().device_default_initialize(&(device.clone() as Arc<dyn Device>));
1000c566df45SLoGin
1001c566df45SLoGin platform_device_manager()
1002c566df45SLoGin .device_add(device.clone() as Arc<dyn PlatformDevice>)
1003c566df45SLoGin .expect("vesa_fb_device_init: platform_device_manager().device_add failed");
1004c566df45SLoGin
1005c566df45SLoGin frame_buffer_manager()
1006c566df45SLoGin .register_fb(device.clone() as Arc<dyn FrameBuffer>)
1007c566df45SLoGin .expect("vesa_fb_device_init: frame_buffer_manager().register_fb failed");
1008c566df45SLoGin
100952da9a59SGnoCiYeH // 加入全局fb表
101052da9a59SGnoCiYeH let mut guard = FRAME_BUFFER_SET.write();
101152da9a59SGnoCiYeH if guard.get(device.fb_id().data() as usize).unwrap().is_some() {
10122eab6dd7S曾俊 warn!(
101352da9a59SGnoCiYeH "vesa_fb_device_init: There is already an element {:?} in the FRAME_BUFFER_SET",
101452da9a59SGnoCiYeH device.fb_id()
101552da9a59SGnoCiYeH );
101652da9a59SGnoCiYeH }
101752da9a59SGnoCiYeH guard[device.fb_id().data() as usize] = Some(device.clone());
101852da9a59SGnoCiYeH
1019c566df45SLoGin // 设置vesa fb的状态为运行中
1020c566df45SLoGin device.inner.lock().fb_state = FbState::Running;
1021c566df45SLoGin });
1022c566df45SLoGin
1023c566df45SLoGin return Ok(());
1024c566df45SLoGin }
1025