xref: /DragonOS/kernel/src/driver/tty/tty_core.rs (revision b5b571e02693d91eb6918d3b7561e088c3e7ee81)
159fdb447SLoGin use core::{
259fdb447SLoGin     fmt::Debug,
359fdb447SLoGin     sync::atomic::{AtomicBool, AtomicUsize},
459fdb447SLoGin };
552da9a59SGnoCiYeH 
652bcb59eSGnoCiYeH use alloc::{collections::LinkedList, string::String, sync::Arc, vec::Vec};
752da9a59SGnoCiYeH use system_error::SystemError;
852da9a59SGnoCiYeH 
952da9a59SGnoCiYeH use crate::{
10f3b05a97SGnoCiYeH     driver::serial::serial8250::send_to_default_serial8250_port,
1152da9a59SGnoCiYeH     libs::{
1252da9a59SGnoCiYeH         rwlock::{RwLock, RwLockReadGuard, RwLockUpgradableGuard, RwLockWriteGuard},
1352da9a59SGnoCiYeH         spinlock::{SpinLock, SpinLockGuard},
1452da9a59SGnoCiYeH         wait_queue::EventWaitQueue,
1552da9a59SGnoCiYeH     },
1652da9a59SGnoCiYeH     mm::VirtAddr,
1752bcb59eSGnoCiYeH     net::event_poll::{EPollEventType, EPollItem},
1852da9a59SGnoCiYeH     process::Pid,
19be60c929SGnoCiYeH     syscall::user_access::{UserBufferReader, UserBufferWriter},
2052da9a59SGnoCiYeH };
2152da9a59SGnoCiYeH 
2252da9a59SGnoCiYeH use super::{
2352da9a59SGnoCiYeH     termios::{ControlMode, PosixTermios, Termios, TtySetTermiosOpt, WindowSize},
2452da9a59SGnoCiYeH     tty_driver::{TtyDriver, TtyDriverSubType, TtyDriverType, TtyOperation},
2552da9a59SGnoCiYeH     tty_ldisc::{
2652da9a59SGnoCiYeH         ntty::{NTtyData, NTtyLinediscipline},
2752da9a59SGnoCiYeH         TtyLineDiscipline,
2852da9a59SGnoCiYeH     },
2952da9a59SGnoCiYeH     tty_port::TtyPort,
3052da9a59SGnoCiYeH     virtual_terminal::{virtual_console::VirtualConsoleData, VIRT_CONSOLES},
3152da9a59SGnoCiYeH };
3252da9a59SGnoCiYeH 
3352da9a59SGnoCiYeH #[derive(Debug)]
3452da9a59SGnoCiYeH pub struct TtyCore {
3552da9a59SGnoCiYeH     core: TtyCoreData,
3652da9a59SGnoCiYeH     /// 线路规程函数集
3752da9a59SGnoCiYeH     line_discipline: Arc<dyn TtyLineDiscipline>,
3852da9a59SGnoCiYeH }
3952da9a59SGnoCiYeH 
4052da9a59SGnoCiYeH impl TtyCore {
4152da9a59SGnoCiYeH     pub fn new(driver: Arc<TtyDriver>, index: usize) -> Arc<Self> {
4252da9a59SGnoCiYeH         let name = driver.tty_line_name(index);
4352da9a59SGnoCiYeH         let termios = driver.init_termios();
4452da9a59SGnoCiYeH         let core = TtyCoreData {
4552da9a59SGnoCiYeH             tty_driver: driver,
4652da9a59SGnoCiYeH             termios: RwLock::new(termios),
4752da9a59SGnoCiYeH             name,
4852da9a59SGnoCiYeH             flags: RwLock::new(TtyFlag::empty()),
4959fdb447SLoGin             count: AtomicUsize::new(0),
5052da9a59SGnoCiYeH             window_size: RwLock::new(WindowSize::default()),
5152da9a59SGnoCiYeH             read_wq: EventWaitQueue::new(),
5252da9a59SGnoCiYeH             write_wq: EventWaitQueue::new(),
5352da9a59SGnoCiYeH             port: RwLock::new(None),
5452da9a59SGnoCiYeH             index,
5552da9a59SGnoCiYeH             ctrl: SpinLock::new(TtyContorlInfo::default()),
5652da9a59SGnoCiYeH             closing: AtomicBool::new(false),
5752da9a59SGnoCiYeH             flow: SpinLock::new(TtyFlowState::default()),
58be60c929SGnoCiYeH             link: None,
5952bcb59eSGnoCiYeH             epitems: SpinLock::new(LinkedList::new()),
6052da9a59SGnoCiYeH         };
6152da9a59SGnoCiYeH 
6252da9a59SGnoCiYeH         return Arc::new(Self {
6352da9a59SGnoCiYeH             core,
6452da9a59SGnoCiYeH             line_discipline: Arc::new(NTtyLinediscipline {
6552da9a59SGnoCiYeH                 data: SpinLock::new(NTtyData::new()),
6652da9a59SGnoCiYeH             }),
6752da9a59SGnoCiYeH         });
6852da9a59SGnoCiYeH     }
6952da9a59SGnoCiYeH 
7052da9a59SGnoCiYeH     #[inline]
7152da9a59SGnoCiYeH     pub fn core(&self) -> &TtyCoreData {
7252da9a59SGnoCiYeH         return &self.core;
7352da9a59SGnoCiYeH     }
7452da9a59SGnoCiYeH 
7552da9a59SGnoCiYeH     #[inline]
7652da9a59SGnoCiYeH     pub fn ldisc(&self) -> Arc<dyn TtyLineDiscipline> {
7752da9a59SGnoCiYeH         self.line_discipline.clone()
7852da9a59SGnoCiYeH     }
7952da9a59SGnoCiYeH 
80f3b05a97SGnoCiYeH     pub fn write_without_serial(&self, buf: &[u8], nr: usize) -> Result<usize, SystemError> {
81f3b05a97SGnoCiYeH         self.core
82f3b05a97SGnoCiYeH             .driver()
83f3b05a97SGnoCiYeH             .driver_funcs()
84f3b05a97SGnoCiYeH             .write(self.core(), buf, nr)
85f3b05a97SGnoCiYeH     }
86f3b05a97SGnoCiYeH 
8752da9a59SGnoCiYeH     pub fn reopen(&self) -> Result<(), SystemError> {
8852da9a59SGnoCiYeH         let tty_core = self.core();
8952da9a59SGnoCiYeH         let driver = tty_core.driver();
9052da9a59SGnoCiYeH 
9152da9a59SGnoCiYeH         if driver.tty_driver_type() == TtyDriverType::Pty
9252da9a59SGnoCiYeH             && driver.tty_driver_sub_type() == TtyDriverSubType::PtyMaster
9352da9a59SGnoCiYeH         {
9452da9a59SGnoCiYeH             return Err(SystemError::EIO);
9552da9a59SGnoCiYeH         }
9652da9a59SGnoCiYeH 
9752da9a59SGnoCiYeH         // if *tty_core.count.read() == 0 {
9852da9a59SGnoCiYeH         //     return Err(SystemError::EAGAIN_OR_EWOULDBLOCK);
9952da9a59SGnoCiYeH         // }
10052da9a59SGnoCiYeH 
10152da9a59SGnoCiYeH         // TODO 判断flags
10252da9a59SGnoCiYeH 
10352da9a59SGnoCiYeH         tty_core.add_count();
10452da9a59SGnoCiYeH 
10552da9a59SGnoCiYeH         Ok(())
10652da9a59SGnoCiYeH     }
10752da9a59SGnoCiYeH 
10852da9a59SGnoCiYeH     #[inline]
10952da9a59SGnoCiYeH     pub fn set_port(&self, port: Arc<dyn TtyPort>) {
11052da9a59SGnoCiYeH         *self.core.port.write() = Some(port);
11152da9a59SGnoCiYeH     }
11252da9a59SGnoCiYeH 
11352da9a59SGnoCiYeH     pub fn tty_start(&self) {
11452da9a59SGnoCiYeH         let mut flow = self.core.flow.lock_irqsave();
11552da9a59SGnoCiYeH         if !flow.stopped || flow.tco_stopped {
11652da9a59SGnoCiYeH             return;
11752da9a59SGnoCiYeH         }
11852da9a59SGnoCiYeH 
11952da9a59SGnoCiYeH         flow.stopped = false;
12052da9a59SGnoCiYeH         let _ = self.start(self.core());
12152da9a59SGnoCiYeH         self.tty_wakeup();
12252da9a59SGnoCiYeH     }
12352da9a59SGnoCiYeH 
12452da9a59SGnoCiYeH     pub fn tty_stop(&self) {
12552da9a59SGnoCiYeH         let mut flow = self.core.flow.lock_irqsave();
12652da9a59SGnoCiYeH         if flow.stopped {
12752da9a59SGnoCiYeH             return;
12852da9a59SGnoCiYeH         }
12952da9a59SGnoCiYeH         flow.stopped = true;
13052da9a59SGnoCiYeH 
13152da9a59SGnoCiYeH         let _ = self.stop(self.core());
13252da9a59SGnoCiYeH     }
13352da9a59SGnoCiYeH 
13452da9a59SGnoCiYeH     pub fn tty_wakeup(&self) {
13559fdb447SLoGin         if self.core.flags().contains(TtyFlag::DO_WRITE_WAKEUP) {
13652da9a59SGnoCiYeH             let _ = self.ldisc().write_wakeup(self.core());
13752da9a59SGnoCiYeH         }
13852da9a59SGnoCiYeH 
13952da9a59SGnoCiYeH         self.core()
14052da9a59SGnoCiYeH             .write_wq
14152da9a59SGnoCiYeH             .wakeup(EPollEventType::EPOLLOUT.bits() as u64);
14252da9a59SGnoCiYeH     }
14352da9a59SGnoCiYeH 
144be60c929SGnoCiYeH     pub fn tty_mode_ioctl(tty: Arc<TtyCore>, cmd: u32, arg: usize) -> Result<usize, SystemError> {
145be60c929SGnoCiYeH         let core = tty.core();
146*b5b571e0SLoGin         let real_tty = if core.driver().tty_driver_type() == TtyDriverType::Pty
147be60c929SGnoCiYeH             && core.driver().tty_driver_sub_type() == TtyDriverSubType::PtyMaster
148be60c929SGnoCiYeH         {
149*b5b571e0SLoGin             core.link().unwrap()
150be60c929SGnoCiYeH         } else {
151*b5b571e0SLoGin             tty
152*b5b571e0SLoGin         };
15352da9a59SGnoCiYeH         match cmd {
15452da9a59SGnoCiYeH             TtyIoctlCmd::TCGETS => {
155*b5b571e0SLoGin                 let termios = PosixTermios::from_kernel_termios(*real_tty.core.termios());
15652da9a59SGnoCiYeH                 let mut user_writer = UserBufferWriter::new(
15752da9a59SGnoCiYeH                     VirtAddr::new(arg).as_ptr::<PosixTermios>(),
15852da9a59SGnoCiYeH                     core::mem::size_of::<PosixTermios>(),
15952da9a59SGnoCiYeH                     true,
16052da9a59SGnoCiYeH                 )?;
16152da9a59SGnoCiYeH 
16252da9a59SGnoCiYeH                 user_writer.copy_one_to_user(&termios, 0)?;
16352da9a59SGnoCiYeH                 return Ok(0);
16452da9a59SGnoCiYeH             }
16552bcb59eSGnoCiYeH             TtyIoctlCmd::TCSETS => {
16652bcb59eSGnoCiYeH                 return TtyCore::core_set_termios(
16752bcb59eSGnoCiYeH                     real_tty,
16852bcb59eSGnoCiYeH                     VirtAddr::new(arg),
16952bcb59eSGnoCiYeH                     TtySetTermiosOpt::TERMIOS_OLD,
17052bcb59eSGnoCiYeH                 );
17152bcb59eSGnoCiYeH             }
17252da9a59SGnoCiYeH             TtyIoctlCmd::TCSETSW => {
173be60c929SGnoCiYeH                 return TtyCore::core_set_termios(
174be60c929SGnoCiYeH                     real_tty,
17552da9a59SGnoCiYeH                     VirtAddr::new(arg),
17652da9a59SGnoCiYeH                     TtySetTermiosOpt::TERMIOS_WAIT | TtySetTermiosOpt::TERMIOS_OLD,
17752da9a59SGnoCiYeH                 );
17852da9a59SGnoCiYeH             }
17952da9a59SGnoCiYeH             _ => {
18052da9a59SGnoCiYeH                 return Err(SystemError::ENOIOCTLCMD);
18152da9a59SGnoCiYeH             }
18252da9a59SGnoCiYeH         }
18352da9a59SGnoCiYeH     }
18452da9a59SGnoCiYeH 
18552da9a59SGnoCiYeH     pub fn core_set_termios(
18652da9a59SGnoCiYeH         tty: Arc<TtyCore>,
18752da9a59SGnoCiYeH         arg: VirtAddr,
18852da9a59SGnoCiYeH         opt: TtySetTermiosOpt,
18952da9a59SGnoCiYeH     ) -> Result<usize, SystemError> {
190be60c929SGnoCiYeH         #[allow(unused_assignments)]
191be60c929SGnoCiYeH         // TERMIOS_TERMIO下会用到
192*b5b571e0SLoGin         let mut tmp_termios = *tty.core().termios();
19352da9a59SGnoCiYeH 
19452da9a59SGnoCiYeH         if opt.contains(TtySetTermiosOpt::TERMIOS_TERMIO) {
19552da9a59SGnoCiYeH             todo!()
19652da9a59SGnoCiYeH         } else {
197be60c929SGnoCiYeH             let user_reader = UserBufferReader::new(
19852da9a59SGnoCiYeH                 arg.as_ptr::<PosixTermios>(),
19952da9a59SGnoCiYeH                 core::mem::size_of::<PosixTermios>(),
20052da9a59SGnoCiYeH                 true,
20152da9a59SGnoCiYeH             )?;
20252da9a59SGnoCiYeH 
203be60c929SGnoCiYeH             let mut term = PosixTermios::default();
204be60c929SGnoCiYeH             user_reader.copy_one_from_user(&mut term, 0)?;
205be60c929SGnoCiYeH 
206be60c929SGnoCiYeH             tmp_termios = term.to_kernel_termios();
20752da9a59SGnoCiYeH         }
20852da9a59SGnoCiYeH 
20952da9a59SGnoCiYeH         if opt.contains(TtySetTermiosOpt::TERMIOS_FLUSH) {
210be60c929SGnoCiYeH             let ld = tty.ldisc();
21152da9a59SGnoCiYeH             let _ = ld.flush_buffer(tty.clone());
21252da9a59SGnoCiYeH         }
21352da9a59SGnoCiYeH 
21452da9a59SGnoCiYeH         if opt.contains(TtySetTermiosOpt::TERMIOS_WAIT) {
21552da9a59SGnoCiYeH             // TODO
21652da9a59SGnoCiYeH         }
21752da9a59SGnoCiYeH 
218be60c929SGnoCiYeH         TtyCore::set_termios_next(tty, tmp_termios)?;
21952da9a59SGnoCiYeH         Ok(0)
22052da9a59SGnoCiYeH     }
22152da9a59SGnoCiYeH 
222be60c929SGnoCiYeH     pub fn set_termios_next(tty: Arc<TtyCore>, new_termios: Termios) -> Result<(), SystemError> {
223be60c929SGnoCiYeH         let mut termios = tty.core().termios_write();
22452da9a59SGnoCiYeH 
225*b5b571e0SLoGin         let old_termios = *termios;
22652da9a59SGnoCiYeH         *termios = new_termios;
22752da9a59SGnoCiYeH         let tmp = termios.control_mode;
22852da9a59SGnoCiYeH         termios.control_mode ^= (tmp ^ old_termios.control_mode) & ControlMode::ADDRB;
22952da9a59SGnoCiYeH 
230be60c929SGnoCiYeH         let ret = tty.set_termios(tty.clone(), old_termios);
23152da9a59SGnoCiYeH         if ret.is_err() {
23252da9a59SGnoCiYeH             termios.control_mode &= ControlMode::HUPCL | ControlMode::CREAD | ControlMode::CLOCAL;
23352da9a59SGnoCiYeH             termios.control_mode |= old_termios.control_mode
23452da9a59SGnoCiYeH                 & !(ControlMode::HUPCL | ControlMode::CREAD | ControlMode::CLOCAL);
23552da9a59SGnoCiYeH             termios.input_speed = old_termios.input_speed;
23652da9a59SGnoCiYeH             termios.output_speed = old_termios.output_speed;
23752da9a59SGnoCiYeH         }
23852da9a59SGnoCiYeH 
23952da9a59SGnoCiYeH         drop(termios);
240be60c929SGnoCiYeH         let ld = tty.ldisc();
24152da9a59SGnoCiYeH         ld.set_termios(tty, Some(old_termios))?;
24252da9a59SGnoCiYeH 
24352da9a59SGnoCiYeH         Ok(())
24452da9a59SGnoCiYeH     }
24552da9a59SGnoCiYeH }
24652da9a59SGnoCiYeH 
247*b5b571e0SLoGin #[derive(Debug, Default)]
24852da9a59SGnoCiYeH pub struct TtyContorlInfo {
24952da9a59SGnoCiYeH     /// 前台进程pid
25052da9a59SGnoCiYeH     pub session: Option<Pid>,
25152da9a59SGnoCiYeH     /// 前台进程组id
25252da9a59SGnoCiYeH     pub pgid: Option<Pid>,
25352da9a59SGnoCiYeH 
25452da9a59SGnoCiYeH     /// packet模式下使用,目前未用到
25552da9a59SGnoCiYeH     pub pktstatus: u8,
25652da9a59SGnoCiYeH     pub packet: bool,
25752da9a59SGnoCiYeH }
25852da9a59SGnoCiYeH 
25952da9a59SGnoCiYeH #[derive(Debug, Default)]
26052da9a59SGnoCiYeH pub struct TtyCoreWriteData {
26152da9a59SGnoCiYeH     /// 写缓冲区
26252da9a59SGnoCiYeH     pub write_buf: Vec<u8>,
26352da9a59SGnoCiYeH     /// 写入数量
26452da9a59SGnoCiYeH     pub write_cnt: usize,
26552da9a59SGnoCiYeH }
26652da9a59SGnoCiYeH 
26752da9a59SGnoCiYeH #[derive(Debug, Default)]
26852da9a59SGnoCiYeH pub struct TtyFlowState {
26952da9a59SGnoCiYeH     /// 表示流控是否被停止
27052da9a59SGnoCiYeH     pub stopped: bool,
27152da9a59SGnoCiYeH     /// 表示 TCO(Transmit Continuous Operation)流控是否被停止
27252da9a59SGnoCiYeH     pub tco_stopped: bool,
27352da9a59SGnoCiYeH }
27452da9a59SGnoCiYeH 
27552da9a59SGnoCiYeH #[derive(Debug)]
27652da9a59SGnoCiYeH pub struct TtyCoreData {
27752da9a59SGnoCiYeH     tty_driver: Arc<TtyDriver>,
27852da9a59SGnoCiYeH     termios: RwLock<Termios>,
27952da9a59SGnoCiYeH     name: String,
28052da9a59SGnoCiYeH     flags: RwLock<TtyFlag>,
28152da9a59SGnoCiYeH     /// 在初始化时即确定不会更改,所以这里不用加锁
28252da9a59SGnoCiYeH     index: usize,
28359fdb447SLoGin     count: AtomicUsize,
28452da9a59SGnoCiYeH     /// 窗口大小
28552da9a59SGnoCiYeH     window_size: RwLock<WindowSize>,
28652da9a59SGnoCiYeH     /// 读等待队列
28752da9a59SGnoCiYeH     read_wq: EventWaitQueue,
28852da9a59SGnoCiYeH     /// 写等待队列
28952da9a59SGnoCiYeH     write_wq: EventWaitQueue,
29052da9a59SGnoCiYeH     /// 端口
29152da9a59SGnoCiYeH     port: RwLock<Option<Arc<dyn TtyPort>>>,
29252da9a59SGnoCiYeH     /// 前台进程
29352da9a59SGnoCiYeH     ctrl: SpinLock<TtyContorlInfo>,
29452da9a59SGnoCiYeH     /// 是否正在关闭
29552da9a59SGnoCiYeH     closing: AtomicBool,
29652da9a59SGnoCiYeH     /// 流控状态
29752da9a59SGnoCiYeH     flow: SpinLock<TtyFlowState>,
298be60c929SGnoCiYeH     /// 链接tty
299be60c929SGnoCiYeH     link: Option<Arc<TtyCore>>,
30052bcb59eSGnoCiYeH     /// epitems
30152bcb59eSGnoCiYeH     epitems: SpinLock<LinkedList<Arc<EPollItem>>>,
30252da9a59SGnoCiYeH }
30352da9a59SGnoCiYeH 
30452da9a59SGnoCiYeH impl TtyCoreData {
30552da9a59SGnoCiYeH     #[inline]
30652da9a59SGnoCiYeH     pub fn driver(&self) -> Arc<TtyDriver> {
30752da9a59SGnoCiYeH         self.tty_driver.clone()
30852da9a59SGnoCiYeH     }
30952da9a59SGnoCiYeH 
31052da9a59SGnoCiYeH     #[inline]
31152da9a59SGnoCiYeH     pub fn flow_irqsave(&self) -> SpinLockGuard<TtyFlowState> {
31252da9a59SGnoCiYeH         self.flow.lock_irqsave()
31352da9a59SGnoCiYeH     }
31452da9a59SGnoCiYeH 
31552da9a59SGnoCiYeH     #[inline]
31652da9a59SGnoCiYeH     pub fn port(&self) -> Option<Arc<dyn TtyPort>> {
31752da9a59SGnoCiYeH         self.port.read().clone()
31852da9a59SGnoCiYeH     }
31952da9a59SGnoCiYeH 
32052da9a59SGnoCiYeH     #[inline]
32152da9a59SGnoCiYeH     pub fn index(&self) -> usize {
32252da9a59SGnoCiYeH         self.index
32352da9a59SGnoCiYeH     }
32452da9a59SGnoCiYeH 
32552da9a59SGnoCiYeH     #[inline]
32652da9a59SGnoCiYeH     pub fn name(&self) -> String {
32752da9a59SGnoCiYeH         self.name.clone()
32852da9a59SGnoCiYeH     }
32952da9a59SGnoCiYeH 
33052da9a59SGnoCiYeH     #[inline]
33152da9a59SGnoCiYeH     pub fn flags(&self) -> TtyFlag {
332*b5b571e0SLoGin         *self.flags.read_irqsave()
33352da9a59SGnoCiYeH     }
33452da9a59SGnoCiYeH 
33552da9a59SGnoCiYeH     #[inline]
33652da9a59SGnoCiYeH     pub fn termios(&self) -> RwLockReadGuard<'_, Termios> {
33752bcb59eSGnoCiYeH         self.termios.read_irqsave()
33852da9a59SGnoCiYeH     }
33952da9a59SGnoCiYeH 
34052da9a59SGnoCiYeH     #[inline]
34152da9a59SGnoCiYeH     pub fn termios_write(&self) -> RwLockWriteGuard<Termios> {
34252bcb59eSGnoCiYeH         self.termios.write_irqsave()
34352da9a59SGnoCiYeH     }
34452da9a59SGnoCiYeH 
34552da9a59SGnoCiYeH     #[inline]
34652da9a59SGnoCiYeH     pub fn set_termios(&self, termios: Termios) {
34759fdb447SLoGin         let mut termios_guard = self.termios_write();
34852da9a59SGnoCiYeH         *termios_guard = termios;
34952da9a59SGnoCiYeH     }
35052da9a59SGnoCiYeH 
35152da9a59SGnoCiYeH     #[inline]
35252da9a59SGnoCiYeH     pub fn add_count(&self) {
35359fdb447SLoGin         self.count
35459fdb447SLoGin             .fetch_add(1, core::sync::atomic::Ordering::SeqCst);
35552da9a59SGnoCiYeH     }
35652da9a59SGnoCiYeH 
35752da9a59SGnoCiYeH     #[inline]
35852da9a59SGnoCiYeH     pub fn read_wq(&self) -> &EventWaitQueue {
35952da9a59SGnoCiYeH         &self.read_wq
36052da9a59SGnoCiYeH     }
36152da9a59SGnoCiYeH 
36252da9a59SGnoCiYeH     #[inline]
36352da9a59SGnoCiYeH     pub fn write_wq(&self) -> &EventWaitQueue {
36452da9a59SGnoCiYeH         &self.write_wq
36552da9a59SGnoCiYeH     }
36652da9a59SGnoCiYeH 
36752da9a59SGnoCiYeH     #[inline]
36852da9a59SGnoCiYeH     pub fn contorl_info_irqsave(&self) -> SpinLockGuard<TtyContorlInfo> {
36952da9a59SGnoCiYeH         self.ctrl.lock_irqsave()
37052da9a59SGnoCiYeH     }
37152da9a59SGnoCiYeH 
37252da9a59SGnoCiYeH     #[inline]
37352da9a59SGnoCiYeH     pub fn window_size_upgradeable(&self) -> RwLockUpgradableGuard<WindowSize> {
37452da9a59SGnoCiYeH         self.window_size.upgradeable_read()
37552da9a59SGnoCiYeH     }
37652da9a59SGnoCiYeH 
37752da9a59SGnoCiYeH     #[inline]
37852da9a59SGnoCiYeH     pub fn window_size(&self) -> RwLockReadGuard<WindowSize> {
37952da9a59SGnoCiYeH         self.window_size.read()
38052da9a59SGnoCiYeH     }
38152da9a59SGnoCiYeH 
38252da9a59SGnoCiYeH     #[inline]
38352da9a59SGnoCiYeH     pub fn is_closing(&self) -> bool {
38452da9a59SGnoCiYeH         self.closing.load(core::sync::atomic::Ordering::SeqCst)
38552da9a59SGnoCiYeH     }
38652da9a59SGnoCiYeH 
38752da9a59SGnoCiYeH     #[inline]
38852da9a59SGnoCiYeH     pub fn vc_data_irqsave(&self) -> SpinLockGuard<VirtualConsoleData> {
38952da9a59SGnoCiYeH         VIRT_CONSOLES[self.index].lock_irqsave()
39052da9a59SGnoCiYeH     }
391be60c929SGnoCiYeH 
392be60c929SGnoCiYeH     #[inline]
393be60c929SGnoCiYeH     pub fn link(&self) -> Option<Arc<TtyCore>> {
394be60c929SGnoCiYeH         self.link.clone()
395be60c929SGnoCiYeH     }
39652bcb59eSGnoCiYeH 
39752bcb59eSGnoCiYeH     #[inline]
39852bcb59eSGnoCiYeH     pub fn add_epitem(&self, epitem: Arc<EPollItem>) {
39952bcb59eSGnoCiYeH         self.epitems.lock().push_back(epitem)
40052bcb59eSGnoCiYeH     }
40152da9a59SGnoCiYeH }
40252da9a59SGnoCiYeH 
40352da9a59SGnoCiYeH /// TTY 核心接口,不同的tty需要各自实现这个trait
40452da9a59SGnoCiYeH pub trait TtyCoreFuncs: Debug + Send + Sync {}
40552da9a59SGnoCiYeH 
40652da9a59SGnoCiYeH impl TtyOperation for TtyCore {
40752da9a59SGnoCiYeH     #[inline]
40852da9a59SGnoCiYeH     fn open(&self, tty: &TtyCoreData) -> Result<(), SystemError> {
40952da9a59SGnoCiYeH         return self.core().tty_driver.driver_funcs().open(tty);
41052da9a59SGnoCiYeH     }
41152da9a59SGnoCiYeH 
41252da9a59SGnoCiYeH     #[inline]
41352da9a59SGnoCiYeH     fn write_room(&self, tty: &TtyCoreData) -> usize {
41452da9a59SGnoCiYeH         return self.core().tty_driver.driver_funcs().write_room(tty);
41552da9a59SGnoCiYeH     }
41652da9a59SGnoCiYeH 
41752da9a59SGnoCiYeH     #[inline]
41852da9a59SGnoCiYeH     fn write(&self, tty: &TtyCoreData, buf: &[u8], nr: usize) -> Result<usize, SystemError> {
419f3b05a97SGnoCiYeH         send_to_default_serial8250_port(buf);
42052da9a59SGnoCiYeH         return self.core().tty_driver.driver_funcs().write(tty, buf, nr);
42152da9a59SGnoCiYeH     }
42252da9a59SGnoCiYeH 
42352da9a59SGnoCiYeH     #[inline]
42452da9a59SGnoCiYeH     fn flush_chars(&self, tty: &TtyCoreData) {
42552da9a59SGnoCiYeH         self.core().tty_driver.driver_funcs().flush_chars(tty);
42652da9a59SGnoCiYeH     }
42752da9a59SGnoCiYeH 
42852da9a59SGnoCiYeH     #[inline]
42952da9a59SGnoCiYeH     fn put_char(&self, tty: &TtyCoreData, ch: u8) -> Result<(), SystemError> {
43052da9a59SGnoCiYeH         return self.core().tty_driver.driver_funcs().put_char(tty, ch);
43152da9a59SGnoCiYeH     }
43252da9a59SGnoCiYeH 
43352da9a59SGnoCiYeH     #[inline]
43452da9a59SGnoCiYeH     fn install(&self, driver: Arc<TtyDriver>, tty: Arc<TtyCore>) -> Result<(), SystemError> {
43552da9a59SGnoCiYeH         return self.core().tty_driver.driver_funcs().install(driver, tty);
43652da9a59SGnoCiYeH     }
43752da9a59SGnoCiYeH 
43852da9a59SGnoCiYeH     #[inline]
43952da9a59SGnoCiYeH     fn start(&self, tty: &TtyCoreData) -> Result<(), SystemError> {
44052da9a59SGnoCiYeH         return self.core().tty_driver.driver_funcs().start(tty);
44152da9a59SGnoCiYeH     }
44252da9a59SGnoCiYeH 
44352da9a59SGnoCiYeH     #[inline]
44452da9a59SGnoCiYeH     fn stop(&self, tty: &TtyCoreData) -> Result<(), SystemError> {
44552da9a59SGnoCiYeH         return self.core().tty_driver.driver_funcs().stop(tty);
44652da9a59SGnoCiYeH     }
44752da9a59SGnoCiYeH 
44852da9a59SGnoCiYeH     #[inline]
44952da9a59SGnoCiYeH     fn ioctl(&self, tty: Arc<TtyCore>, cmd: u32, arg: usize) -> Result<(), SystemError> {
45052da9a59SGnoCiYeH         return self.core().tty_driver.driver_funcs().ioctl(tty, cmd, arg);
45152da9a59SGnoCiYeH     }
45252da9a59SGnoCiYeH 
45352da9a59SGnoCiYeH     #[inline]
45452da9a59SGnoCiYeH     fn chars_in_buffer(&self) -> usize {
45552da9a59SGnoCiYeH         return self.core().tty_driver.driver_funcs().chars_in_buffer();
45652da9a59SGnoCiYeH     }
45752da9a59SGnoCiYeH 
45852da9a59SGnoCiYeH     #[inline]
45952da9a59SGnoCiYeH     fn set_termios(&self, tty: Arc<TtyCore>, old_termios: Termios) -> Result<(), SystemError> {
46052da9a59SGnoCiYeH         return self
46152da9a59SGnoCiYeH             .core()
46252da9a59SGnoCiYeH             .tty_driver
46352da9a59SGnoCiYeH             .driver_funcs()
46452da9a59SGnoCiYeH             .set_termios(tty, old_termios);
46552da9a59SGnoCiYeH     }
46652da9a59SGnoCiYeH }
46752da9a59SGnoCiYeH 
46852da9a59SGnoCiYeH bitflags! {
46952da9a59SGnoCiYeH     pub struct TtyFlag: u32 {
47052da9a59SGnoCiYeH         /// 终端被节流
47152da9a59SGnoCiYeH         const THROTTLED		= 1 << 0;
47252da9a59SGnoCiYeH         /// 终端输入输出错误状态
47352da9a59SGnoCiYeH         const IO_ERROR		= 1 << 1;
47452da9a59SGnoCiYeH         /// 终端的其他一方已关闭
47552da9a59SGnoCiYeH         const OTHER_CLOSED	= 1 << 2;
47652da9a59SGnoCiYeH         /// 终端处于独占状态
47752da9a59SGnoCiYeH         const EXCLUSIVE		= 1 << 3;
47852da9a59SGnoCiYeH         /// 终端执行写唤醒操作
47952da9a59SGnoCiYeH         const DO_WRITE_WAKEUP	= 1 << 5;
48052da9a59SGnoCiYeH         /// 终端线路驱动程序已打开
48152da9a59SGnoCiYeH         const LDISC_OPEN		= 1 << 11;
48252da9a59SGnoCiYeH         /// 终端伪终端设备已锁定
48352da9a59SGnoCiYeH         const PTY_LOCK		= 1 << 16;
48452da9a59SGnoCiYeH         /// 终端禁用写分裂操作
48552da9a59SGnoCiYeH         const NO_WRITE_SPLIT	= 1 << 17;
48652da9a59SGnoCiYeH         /// 终端挂断(挂起)状态
48752da9a59SGnoCiYeH         const HUPPED		= 1 << 18;
48852da9a59SGnoCiYeH         /// 终端正在挂断(挂起)
48952da9a59SGnoCiYeH         const HUPPING		= 1 << 19;
49052da9a59SGnoCiYeH         /// 终端线路驱动程序正在更改
49152da9a59SGnoCiYeH         const LDISC_CHANGING	= 1 << 20;
49252da9a59SGnoCiYeH         /// 终端线路驱动程序已停止
49352da9a59SGnoCiYeH         const LDISC_HALTED	= 1 << 22;
49452da9a59SGnoCiYeH     }
49552da9a59SGnoCiYeH }
49652da9a59SGnoCiYeH 
49752da9a59SGnoCiYeH #[derive(Debug, PartialEq)]
49852da9a59SGnoCiYeH pub enum EchoOperation {
49952da9a59SGnoCiYeH     /// 开始特殊操作。
50052da9a59SGnoCiYeH     Start,
50152da9a59SGnoCiYeH     /// 向后移动光标列。
50252da9a59SGnoCiYeH     MoveBackCol,
50352da9a59SGnoCiYeH     /// 设置规范模式下的列位置。
50452da9a59SGnoCiYeH     SetCanonCol,
50552da9a59SGnoCiYeH     /// 擦除制表符。
50652da9a59SGnoCiYeH     EraseTab,
50752da9a59SGnoCiYeH 
50852da9a59SGnoCiYeH     Undefined(u8),
50952da9a59SGnoCiYeH }
51052da9a59SGnoCiYeH 
51152da9a59SGnoCiYeH impl EchoOperation {
51252da9a59SGnoCiYeH     pub fn from_u8(num: u8) -> EchoOperation {
51352da9a59SGnoCiYeH         match num {
51452da9a59SGnoCiYeH             0xff => Self::Start,
51552da9a59SGnoCiYeH             0x80 => Self::MoveBackCol,
51652da9a59SGnoCiYeH             0x81 => Self::SetCanonCol,
51752da9a59SGnoCiYeH             0x82 => Self::EraseTab,
51852da9a59SGnoCiYeH             _ => Self::Undefined(num),
51952da9a59SGnoCiYeH         }
52052da9a59SGnoCiYeH     }
52152da9a59SGnoCiYeH 
52252da9a59SGnoCiYeH     pub fn to_u8(&self) -> u8 {
52352da9a59SGnoCiYeH         match *self {
52452da9a59SGnoCiYeH             EchoOperation::Start => 0xff,
52552da9a59SGnoCiYeH             EchoOperation::MoveBackCol => 0x80,
52652da9a59SGnoCiYeH             EchoOperation::SetCanonCol => 0x81,
52752da9a59SGnoCiYeH             EchoOperation::EraseTab => 0x82,
52852da9a59SGnoCiYeH             EchoOperation::Undefined(num) => num,
52952da9a59SGnoCiYeH         }
53052da9a59SGnoCiYeH     }
53152da9a59SGnoCiYeH }
53252da9a59SGnoCiYeH 
53352da9a59SGnoCiYeH pub struct TtyIoctlCmd;
53452da9a59SGnoCiYeH 
53552da9a59SGnoCiYeH #[allow(dead_code)]
53652da9a59SGnoCiYeH impl TtyIoctlCmd {
53752da9a59SGnoCiYeH     /// 获取终端参数
53852da9a59SGnoCiYeH     pub const TCGETS: u32 = 0x5401;
53952da9a59SGnoCiYeH     /// 设置终端参数
54052da9a59SGnoCiYeH     pub const TCSETS: u32 = 0x5402;
54152da9a59SGnoCiYeH     /// 设置终端参数并等待所有输出完成
54252da9a59SGnoCiYeH     pub const TCSETSW: u32 = 0x5403;
54352da9a59SGnoCiYeH     /// 设置终端参数并且等待所有输出完成,但在这之前将终端清空
54452da9a59SGnoCiYeH     pub const TCSETSF: u32 = 0x5404;
54552da9a59SGnoCiYeH     /// 获取终端参数
54652da9a59SGnoCiYeH     pub const TCGETA: u32 = 0x5405;
54752da9a59SGnoCiYeH     /// 设置终端参数
54852da9a59SGnoCiYeH     pub const TCSETA: u32 = 0x5406;
54952da9a59SGnoCiYeH     /// 设置终端参数并等待所有输出完成
55052da9a59SGnoCiYeH     pub const TCSETAW: u32 = 0x5407;
55152da9a59SGnoCiYeH     /// 设置终端参数并且等待所有输出完成,但在这之前将终端清空
55252da9a59SGnoCiYeH     pub const TCSETAF: u32 = 0x5408;
55352da9a59SGnoCiYeH     /// 发送零字节,等待所有输出完成
55452da9a59SGnoCiYeH     pub const TCSBRK: u32 = 0x5409;
55552da9a59SGnoCiYeH     /// 控制终端的流控
55652da9a59SGnoCiYeH     pub const TCXONC: u32 = 0x540A;
55752da9a59SGnoCiYeH     /// 刷新输入/输出缓冲区或者丢弃输入缓冲区
55852da9a59SGnoCiYeH     pub const TCFLSH: u32 = 0x540B;
55952da9a59SGnoCiYeH     /// 设置设备为独占模式
56052da9a59SGnoCiYeH     pub const TIOCEXCL: u32 = 0x540C;
56152da9a59SGnoCiYeH     /// 设置设备为非独占模式
56252da9a59SGnoCiYeH     pub const TIOCNXCL: u32 = 0x540D;
56352da9a59SGnoCiYeH     /// 设置当前进程的控制终端
56452da9a59SGnoCiYeH     pub const TIOCSCTTY: u32 = 0x540E;
56552da9a59SGnoCiYeH     /// 获取前台进程组
56652da9a59SGnoCiYeH     pub const TIOCGPGRP: u32 = 0x540F;
56752da9a59SGnoCiYeH     ///设置前台进程组
56852da9a59SGnoCiYeH     pub const TIOCSPGRP: u32 = 0x5410;
56952da9a59SGnoCiYeH     /// 获取输出队列的字节数
57052da9a59SGnoCiYeH     pub const TIOCOUTQ: u32 = 0x5411;
57152da9a59SGnoCiYeH     /// 模拟从终端输入字符
57252da9a59SGnoCiYeH     pub const TIOCSTI: u32 = 0x5412;
57352da9a59SGnoCiYeH     /// 获取窗口大小
57452da9a59SGnoCiYeH     pub const TIOCGWINSZ: u32 = 0x5413;
57552da9a59SGnoCiYeH     /// 设置窗口大小
57652da9a59SGnoCiYeH     pub const TIOCSWINSZ: u32 = 0x5414;
57752da9a59SGnoCiYeH     /// 获取终端控制信号的状态
57852da9a59SGnoCiYeH     pub const TIOCMGET: u32 = 0x5415;
57952da9a59SGnoCiYeH     /// 设置终端控制信号的位
58052da9a59SGnoCiYeH     pub const TIOCMBIS: u32 = 0x5416;
58152da9a59SGnoCiYeH     /// 清除终端控制信号的位
58252da9a59SGnoCiYeH     pub const TIOCMBIC: u32 = 0x5417;
58352da9a59SGnoCiYeH     /// 设置终端控制信号的状态
58452da9a59SGnoCiYeH     pub const TIOCMSET: u32 = 0x5418;
58552da9a59SGnoCiYeH     /// 获取软件载波状态
58652da9a59SGnoCiYeH     pub const TIOCGSOFTCAR: u32 = 0x5419;
58752da9a59SGnoCiYeH     /// 设置软件载波状态
58852da9a59SGnoCiYeH     pub const TIOCSSOFTCAR: u32 = 0x541A;
58952da9a59SGnoCiYeH     /// 获取输入队列的字节数
59052da9a59SGnoCiYeH     pub const FIONREAD: u32 = 0x541B;
59152da9a59SGnoCiYeH     /// Linux 特有命令
59252da9a59SGnoCiYeH     pub const TIOCLINUX: u32 = 0x541C;
59352da9a59SGnoCiYeH     /// 获取控制台设备
59452da9a59SGnoCiYeH     pub const TIOCCONS: u32 = 0x541D;
59552da9a59SGnoCiYeH     /// 获取串行设备参数
59652da9a59SGnoCiYeH     pub const TIOCGSERIAL: u32 = 0x541E;
59752da9a59SGnoCiYeH     /// 设置串行设备参数
59852da9a59SGnoCiYeH     pub const TIOCSSERIAL: u32 = 0x541F;
59952da9a59SGnoCiYeH     /// 设置套接字的报文模式
60052da9a59SGnoCiYeH     pub const TIOCPKT: u32 = 0x5420;
60152da9a59SGnoCiYeH     /// 设置非阻塞 I/O
60252da9a59SGnoCiYeH     pub const FIONBIO: u32 = 0x5421;
60352da9a59SGnoCiYeH     /// 清除控制终端
60452da9a59SGnoCiYeH     pub const TIOCNOTTY: u32 = 0x5422;
60552da9a59SGnoCiYeH     /// 设置终端线路驱动器
60652da9a59SGnoCiYeH     pub const TIOCSETD: u32 = 0x5423;
60752da9a59SGnoCiYeH     /// 获取终端线路驱动器
60852da9a59SGnoCiYeH     pub const TIOCGETD: u32 = 0x5424;
60952da9a59SGnoCiYeH     /// 发送终止条件
61052da9a59SGnoCiYeH     pub const TCSBRKP: u32 = 0x5425;
61152da9a59SGnoCiYeH     /// 开始发送零比特
61252da9a59SGnoCiYeH     pub const TIOCSBRK: u32 = 0x5427;
61352da9a59SGnoCiYeH     /// 停止发送零比特
61452da9a59SGnoCiYeH     pub const TIOCCBRK: u32 = 0x5428;
61552da9a59SGnoCiYeH     /// Return the session ID of FD
61652da9a59SGnoCiYeH     pub const TIOCGSID: u32 = 0x5429;
61752da9a59SGnoCiYeH }
618