xref: /DragonOS/kernel/src/driver/tty/tty_driver.rs (revision 52dc4c3ee87b31fee4f567f776fd8ad58fbfd9a5)
1 use core::fmt::Debug;
2 
3 use alloc::{
4     string::{String, ToString},
5     sync::{Arc, Weak},
6     vec::Vec,
7 };
8 use hashbrown::HashMap;
9 use ida::IdAllocator;
10 use log::warn;
11 use system_error::SystemError;
12 
13 use crate::{
14     driver::{
15         base::{
16             char::CharDevOps,
17             device::{
18                 device_number::{DeviceNumber, Major},
19                 device_register,
20                 driver::Driver,
21                 IdTable,
22             },
23             kobject::KObject,
24         },
25         tty::tty_port::TtyPortState,
26     },
27     filesystem::devfs::devfs_register,
28     libs::{
29         lazy_init::Lazy,
30         rwlock::RwLock,
31         spinlock::{SpinLock, SpinLockGuard},
32     },
33 };
34 
35 use super::{
36     termios::{Termios, WindowSize},
37     tty_core::{TtyCore, TtyCoreData},
38     tty_device::TtyDevice,
39     tty_ldisc::TtyLdiscManager,
40     tty_port::{DefaultTtyPort, TtyPort},
41 };
42 
43 lazy_static! {
44     pub static ref TTY_DRIVERS: SpinLock<Vec<Arc<TtyDriver>>> = SpinLock::new(Vec::new());
45 }
46 
47 pub enum TtyDriverPrivateData {
48     Unused,
49     /// true表示主设备 false表示从设备
50     Pty(bool),
51 }
52 
53 pub struct TtyDriverManager;
54 impl TtyDriverManager {
55     pub fn lookup_tty_driver(dev_num: DeviceNumber) -> Option<(usize, Arc<TtyDriver>)> {
56         let drivers_guard = TTY_DRIVERS.lock();
57         for driver in drivers_guard.iter() {
58             let base = DeviceNumber::new(driver.major, driver.minor_start);
59             if dev_num < base || dev_num.data() > base.data() + driver.device_count {
60                 continue;
61             }
62             return Some(((dev_num.data() - base.data()) as usize, driver.clone()));
63         }
64 
65         None
66     }
67 
68     /// ## 注册驱动
69     pub fn tty_register_driver(mut driver: TtyDriver) -> Result<Arc<TtyDriver>, SystemError> {
70         // 查看是否注册设备号
71         if driver.major == Major::UNNAMED_MAJOR {
72             let dev_num = CharDevOps::alloc_chardev_region(
73                 driver.minor_start,
74                 driver.device_count,
75                 driver.name,
76             )?;
77             driver.major = dev_num.major();
78             driver.minor_start = dev_num.minor();
79         } else {
80             let dev_num = DeviceNumber::new(driver.major, driver.minor_start);
81             CharDevOps::register_chardev_region(dev_num, driver.device_count, driver.name)?;
82         }
83 
84         driver.flags |= TtyDriverFlag::TTY_DRIVER_INSTALLED;
85 
86         // 加入全局TtyDriver表
87         let driver = Arc::new(driver);
88         driver.self_ref.init(Arc::downgrade(&driver));
89         TTY_DRIVERS.lock().push(driver.clone());
90 
91         // TODO: 加入procfs?
92 
93         Ok(driver)
94     }
95 }
96 
97 /// tty 驱动程序的与设备相关的数据
98 pub trait TtyDriverPrivateField: Debug + Send + Sync {}
99 pub trait TtyCorePrivateField: Debug + Send + Sync {}
100 
101 #[allow(dead_code)]
102 #[derive(Debug)]
103 #[cast_to([sync] Driver)]
104 pub struct TtyDriver {
105     /// /proc/tty中使用的驱动程序名称
106     driver_name: String,
107     /// 用于构造/dev节点名称,例如name设置为tty,则按照name_base分配节点tty0,tty1等
108     name: &'static str,
109     /// 命名基数
110     name_base: usize,
111     /// 主设备号
112     major: Major,
113     /// 起始次设备号
114     minor_start: u32,
115     /// 最多支持的tty数量
116     device_count: u32,
117     /// tty驱动程序类型
118     tty_driver_type: TtyDriverType,
119     /// 驱动程序子类型
120     tty_driver_sub_type: TtyDriverSubType,
121     /// 每个tty的默认termios
122     init_termios: Termios,
123     /// 懒加载termios,在tty设备关闭时,会将termios按照设备的index保存进这个集合,以便下次打开使用
124     saved_termios: Vec<Termios>,
125     /// 驱动程序标志
126     flags: TtyDriverFlag,
127     /// pty链接此driver的入口
128     other_pty_driver: RwLock<Weak<TtyDriver>>,
129     /// 具体类型的tty驱动方法
130     driver_funcs: Arc<dyn TtyOperation>,
131     /// 管理的tty设备列表
132     ttys: SpinLock<HashMap<usize, Arc<TtyCore>>>,
133     /// 管理的端口列表
134     ports: RwLock<Vec<Arc<dyn TtyPort>>>,
135     /// 与设备相关的私有数据
136     private_field: Option<Arc<dyn TtyDriverPrivateField>>,
137     /// id分配器
138     ida: SpinLock<IdAllocator>,
139     self_ref: Lazy<Weak<Self>>,
140 }
141 
142 impl TtyDriver {
143     #[allow(clippy::too_many_arguments)]
144     pub fn new(
145         count: u32,
146         node_name: &'static str,
147         node_name_base: usize,
148         major: Major,
149         minor_start: u32,
150         tty_driver_type: TtyDriverType,
151         default_termios: Termios,
152         driver_funcs: Arc<dyn TtyOperation>,
153         private_field: Option<Arc<dyn TtyDriverPrivateField>>,
154     ) -> Self {
155         let mut ports: Vec<Arc<dyn TtyPort>> = Vec::with_capacity(count as usize);
156         for _ in 0..count {
157             ports.push(Arc::new(DefaultTtyPort::new()))
158         }
159         TtyDriver {
160             driver_name: Default::default(),
161             name: node_name,
162             name_base: node_name_base,
163             major,
164             minor_start,
165             device_count: count,
166             tty_driver_type,
167             tty_driver_sub_type: Default::default(),
168             init_termios: default_termios,
169             flags: TtyDriverFlag::empty(),
170             other_pty_driver: Default::default(),
171             driver_funcs,
172             ttys: SpinLock::new(HashMap::new()),
173             saved_termios: Vec::with_capacity(count as usize),
174             ports: RwLock::new(ports),
175             private_field,
176             ida: SpinLock::new(IdAllocator::new(0, count as usize).unwrap()),
177             self_ref: Lazy::new(),
178         }
179     }
180 
181     pub fn tty_line_name(&self, index: usize) -> String {
182         if self
183             .flags
184             .contains(TtyDriverFlag::TTY_DRIVER_UNNUMBERED_NODE)
185         {
186             return self.name.to_string();
187         } else {
188             return format!("{}{}", self.name, index + self.name_base);
189         }
190     }
191 
192     pub fn add_tty(&self, tty_core: Arc<TtyCore>) {
193         self.ttys.lock().insert(tty_core.core().index(), tty_core);
194     }
195 
196     #[inline]
197     pub fn driver_funcs(&self) -> Arc<dyn TtyOperation> {
198         self.driver_funcs.clone()
199     }
200 
201     /// ## 获取该驱动对应的设备的设备号
202     #[inline]
203     pub fn device_number(&self, index: usize) -> Option<DeviceNumber> {
204         if index >= self.device_count as usize {
205             return None;
206         }
207         Some(DeviceNumber::new(
208             self.major,
209             self.minor_start + index as u32,
210         ))
211     }
212 
213     fn self_ref(&self) -> Arc<Self> {
214         self.self_ref.get().upgrade().unwrap()
215     }
216 
217     #[inline]
218     pub fn init_termios(&self) -> Termios {
219         self.init_termios
220     }
221 
222     #[inline]
223     pub fn init_termios_mut(&mut self) -> &mut Termios {
224         &mut self.init_termios
225     }
226 
227     #[inline]
228     pub fn other_pty_driver(&self) -> Option<Arc<TtyDriver>> {
229         self.other_pty_driver.read().upgrade()
230     }
231 
232     pub fn set_other_pty_driver(&self, driver: Weak<TtyDriver>) {
233         *self.other_pty_driver.write() = driver
234     }
235 
236     #[inline]
237     pub fn set_subtype(&mut self, tp: TtyDriverSubType) {
238         self.tty_driver_sub_type = tp;
239     }
240 
241     #[inline]
242     pub fn ttys(&self) -> SpinLockGuard<HashMap<usize, Arc<TtyCore>>> {
243         self.ttys.lock()
244     }
245 
246     #[inline]
247     pub fn saved_termios(&self) -> &Vec<Termios> {
248         &self.saved_termios
249     }
250 
251     #[inline]
252     pub fn flags(&self) -> TtyDriverFlag {
253         self.flags
254     }
255 
256     #[inline]
257     fn lookup_tty(&self, index: usize) -> Option<Arc<TtyCore>> {
258         let ret = self
259             .driver_funcs()
260             .lookup(index, TtyDriverPrivateData::Unused);
261         if let Err(SystemError::ENOSYS) = ret {
262             let device_guard = self.ttys.lock();
263             return device_guard.get(&index).cloned();
264         }
265         ret.ok()
266     }
267 
268     pub fn standard_install(&self, tty_core: Arc<TtyCore>) -> Result<(), SystemError> {
269         let tty = tty_core.core();
270         tty.init_termios();
271         // TODO:设置termios波特率?
272 
273         tty.add_count();
274 
275         self.ttys.lock().insert(tty.index(), tty_core);
276 
277         Ok(())
278     }
279 
280     fn driver_install_tty(&self, tty: Arc<TtyCore>) -> Result<(), SystemError> {
281         let res = tty.install(self.self_ref(), tty.clone());
282 
283         if let Err(err) = res {
284             if err == SystemError::ENOSYS {
285                 return self.standard_install(tty);
286             } else {
287                 return Err(err);
288             }
289         }
290 
291         self.add_tty(tty);
292 
293         Ok(())
294     }
295 
296     pub fn init_tty_device(&self, index: Option<usize>) -> Result<Arc<TtyCore>, SystemError> {
297         // 如果传入的index为None,那么就自动分配index
298         let idx: usize;
299         if let Some(i) = index {
300             if self.ida.lock().exists(i) {
301                 return Err(SystemError::EINVAL);
302             }
303             idx = i;
304         } else {
305             idx = self.ida.lock().alloc().ok_or(SystemError::EBUSY)?;
306         }
307 
308         let tty = TtyCore::new(self.self_ref(), idx);
309 
310         self.driver_install_tty(tty.clone())?;
311 
312         let core = tty.core();
313 
314         if core.port().is_none() {
315             let ports = self.ports.read();
316             ports[core.index()].setup_internal_tty(Arc::downgrade(&tty));
317             tty.set_port(ports[core.index()].clone());
318         }
319 
320         TtyLdiscManager::ldisc_setup(tty.clone(), tty.core().link())?;
321 
322         // 在devfs创建对应的文件
323 
324         let device = TtyDevice::new(
325             core.name().clone(),
326             IdTable::new(self.tty_line_name(idx), Some(*core.device_number())),
327             super::tty_device::TtyType::Tty,
328         );
329 
330         devfs_register(device.name_ref(), device.clone())?;
331         device_register(device)?;
332         Ok(tty)
333     }
334 
335     /// ## 通过设备号找到对应驱动并且初始化Tty
336     pub fn open_tty(&self, index: Option<usize>) -> Result<Arc<TtyCore>, SystemError> {
337         let mut tty: Option<Arc<TtyCore>> = None;
338 
339         if index.is_some() {
340             if let Some(t) = self.lookup_tty(index.unwrap()) {
341                 if t.core().port().is_none() {
342                     warn!("{} port is None", t.core().name());
343                 } else if t.core().port().unwrap().state() == TtyPortState::KOPENED {
344                     return Err(SystemError::EBUSY);
345                 }
346 
347                 t.reopen()?;
348                 tty = Some(t);
349             }
350         }
351         if tty.is_none() {
352             tty = Some(self.init_tty_device(index)?);
353         }
354         let tty = tty.ok_or(SystemError::ENODEV)?;
355 
356         return Ok(tty);
357     }
358 
359     pub fn tty_driver_type(&self) -> TtyDriverType {
360         self.tty_driver_type
361     }
362 
363     pub fn tty_driver_sub_type(&self) -> TtyDriverSubType {
364         self.tty_driver_sub_type
365     }
366 }
367 
368 impl KObject for TtyDriver {
369     fn as_any_ref(&self) -> &dyn core::any::Any {
370         todo!()
371     }
372 
373     fn set_inode(&self, _inode: Option<alloc::sync::Arc<crate::filesystem::kernfs::KernFSInode>>) {
374         todo!()
375     }
376 
377     fn inode(&self) -> Option<alloc::sync::Arc<crate::filesystem::kernfs::KernFSInode>> {
378         todo!()
379     }
380 
381     fn parent(&self) -> Option<alloc::sync::Weak<dyn KObject>> {
382         todo!()
383     }
384 
385     fn set_parent(&self, _parent: Option<alloc::sync::Weak<dyn KObject>>) {
386         todo!()
387     }
388 
389     fn kset(&self) -> Option<alloc::sync::Arc<crate::driver::base::kset::KSet>> {
390         todo!()
391     }
392 
393     fn set_kset(&self, _kset: Option<alloc::sync::Arc<crate::driver::base::kset::KSet>>) {
394         todo!()
395     }
396 
397     fn kobj_type(&self) -> Option<&'static dyn crate::driver::base::kobject::KObjType> {
398         todo!()
399     }
400 
401     fn set_kobj_type(&self, _ktype: Option<&'static dyn crate::driver::base::kobject::KObjType>) {
402         todo!()
403     }
404 
405     fn name(&self) -> alloc::string::String {
406         todo!()
407     }
408 
409     fn set_name(&self, _name: alloc::string::String) {
410         todo!()
411     }
412 
413     fn kobj_state(
414         &self,
415     ) -> crate::libs::rwlock::RwLockReadGuard<crate::driver::base::kobject::KObjectState> {
416         todo!()
417     }
418 
419     fn kobj_state_mut(
420         &self,
421     ) -> crate::libs::rwlock::RwLockWriteGuard<crate::driver::base::kobject::KObjectState> {
422         todo!()
423     }
424 
425     fn set_kobj_state(&self, _state: crate::driver::base::kobject::KObjectState) {
426         todo!()
427     }
428 }
429 
430 impl Driver for TtyDriver {
431     fn id_table(&self) -> Option<crate::driver::base::device::IdTable> {
432         todo!()
433     }
434 
435     fn devices(
436         &self,
437     ) -> alloc::vec::Vec<alloc::sync::Arc<dyn crate::driver::base::device::Device>> {
438         todo!()
439     }
440 
441     fn add_device(&self, _device: alloc::sync::Arc<dyn crate::driver::base::device::Device>) {
442         todo!()
443     }
444 
445     fn delete_device(&self, _device: &alloc::sync::Arc<dyn crate::driver::base::device::Device>) {
446         todo!()
447     }
448 
449     fn set_bus(&self, _bus: Option<alloc::sync::Weak<dyn crate::driver::base::device::bus::Bus>>) {
450         todo!()
451     }
452 }
453 
454 pub trait TtyOperation: Sync + Send + Debug {
455     fn install(&self, _driver: Arc<TtyDriver>, _tty: Arc<TtyCore>) -> Result<(), SystemError> {
456         return Err(SystemError::ENOSYS);
457     }
458 
459     fn open(&self, tty: &TtyCoreData) -> Result<(), SystemError>;
460 
461     /// ## 获取可写字符数
462     fn write_room(&self, _tty: &TtyCoreData) -> usize {
463         // 默认
464         2048
465     }
466 
467     fn write(&self, tty: &TtyCoreData, buf: &[u8], nr: usize) -> Result<usize, SystemError>;
468 
469     fn flush_chars(&self, tty: &TtyCoreData);
470 
471     fn put_char(&self, _tty: &TtyCoreData, _ch: u8) -> Result<(), SystemError> {
472         Err(SystemError::ENOSYS)
473     }
474 
475     fn start(&self, _tty: &TtyCoreData) -> Result<(), SystemError> {
476         Err(SystemError::ENOSYS)
477     }
478 
479     fn stop(&self, _tty: &TtyCoreData) -> Result<(), SystemError> {
480         Err(SystemError::ENOSYS)
481     }
482 
483     fn flush_buffer(&self, _tty: &TtyCoreData) -> Result<(), SystemError> {
484         Err(SystemError::ENOSYS)
485     }
486 
487     fn ioctl(&self, tty: Arc<TtyCore>, cmd: u32, arg: usize) -> Result<(), SystemError>;
488 
489     fn chars_in_buffer(&self) -> usize {
490         0
491     }
492 
493     fn set_termios(&self, _tty: Arc<TtyCore>, _old_termios: Termios) -> Result<(), SystemError> {
494         Err(SystemError::ENOSYS)
495     }
496 
497     fn lookup(
498         &self,
499         _index: usize,
500         _priv_data: TtyDriverPrivateData,
501     ) -> Result<Arc<TtyCore>, SystemError> {
502         Err(SystemError::ENOSYS)
503     }
504 
505     fn close(&self, tty: Arc<TtyCore>) -> Result<(), SystemError>;
506 
507     fn resize(&self, _tty: Arc<TtyCore>, _winsize: WindowSize) -> Result<(), SystemError> {
508         Err(SystemError::ENOSYS)
509     }
510 }
511 
512 #[allow(dead_code)]
513 #[derive(Debug, PartialEq, Copy, Clone)]
514 pub enum TtyDriverType {
515     System,
516     Console,
517     Serial,
518     Pty,
519     Scc,
520     Syscons,
521 }
522 
523 #[allow(dead_code)]
524 #[derive(Debug, PartialEq, Copy, Clone)]
525 pub enum TtyDriverSubType {
526     Undefined,
527     Tty,
528     Console,
529     Syscons,
530     Sysptmx,
531     PtyMaster,
532     PtySlave,
533     SerialNormal,
534 }
535 
536 impl Default for TtyDriverSubType {
537     fn default() -> Self {
538         Self::Undefined
539     }
540 }
541 
542 bitflags! {
543     pub struct TtyDriverFlag: u32 {
544         /// 表示 tty 驱动程序已安装
545         const TTY_DRIVER_INSTALLED		= 0x0001;
546         /// 请求 tty 层在最后一个进程关闭设备时重置 termios 设置
547         const TTY_DRIVER_RESET_TERMIOS	= 0x0002;
548         /// 表示驱动程序将保证在设置了该标志的 tty 上不设置任何特殊字符处理标志(原模式)
549         const TTY_DRIVER_REAL_RAW		    = 0x0004;
550 
551         /// 以下四个标志位为内存分配相关,目前设计无需使用
552         const TTY_DRIVER_DYNAMIC_DEV		= 0x0008;
553         const TTY_DRIVER_DEVPTS_MEM		= 0x0010;
554         const TTY_DRIVER_HARDWARE_BREAK	= 0x0020;
555         const TTY_DRIVER_DYNAMIC_ALLOC	= 0x0040;
556 
557         /// 表示不创建带有编号的 /dev 节点。
558         /// 例如,创建 /dev/ttyprintk 而不是 /dev/ttyprintk0。仅在为单个 tty 设备分配驱动程序时适用。
559         const TTY_DRIVER_UNNUMBERED_NODE	= 0x0080;
560     }
561 }
562