]>
Zhao Yanbai Git Server - minix.git/blob - drivers/tty/tty.c
5bcc05b7fe15e10cb3b7d2ae4e5547e197c5a1ff
1 /* This file contains the terminal driver, both for the IBM console and regular
2 * ASCII terminals. It handles only the device-independent part of a TTY, the
3 * device dependent parts are in console.c, rs232.c, etc. This file contains
4 * two main entry points, tty_task() and tty_wakeup(), and several minor entry
5 * points for use by the device-dependent code.
7 * The device-independent part accepts "keyboard" input from the device-
8 * dependent part, performs input processing (special key interpretation),
9 * and sends the input to a process reading from the TTY. Output to a TTY
10 * is sent to the device-dependent code for output processing and "screen"
11 * display. Input processing is done by the device by calling 'in_process'
12 * on the input characters, output processing may be done by the device itself
13 * or by calling 'out_process'. The TTY takes care of input queuing, the
14 * device does the output queuing. If a device receives an external signal,
15 * like an interrupt, then it causes tty_wakeup() to be run by the CLOCK task
16 * to, you guessed it, wake up the TTY to check if input or output can
19 * The valid messages and their parameters are:
21 * notify from HARDWARE: output has been completed or input has arrived
22 * notify from SYSTEM : e.g., MINIX wants to shutdown; run code to
24 * DEV_READ: a process wants to read from a terminal
25 * DEV_WRITE: a process wants to write on a terminal
26 * DEV_IOCTL: a process wants to change a terminal's parameters
27 * DEV_OPEN: a tty line has been opened
28 * DEV_CLOSE: a tty line has been closed
29 * DEV_SELECT: start select notification request
30 * CANCEL: terminate a previous incomplete system call immediately
32 * m_type DEVICE USER_ENDPT COUNT POSITION IO_GRANT
33 * -----------------------------------------------------------------
34 * | HARD_INT | | | | | |
35 * |-------------+---------+---------+---------+---------+---------|
36 * | DEV_READ |minor dev| proc nr | count | | grant |
37 * |-------------+---------+---------+---------+---------+---------|
38 * | DEV_WRITE |minor dev| proc nr | count | | grant |
39 * |-------------+---------+---------+---------+---------+---------|
40 * | DEV_IOCTL |minor dev| proc nr |func code|user proc| |
41 * |-------------+---------+---------+---------+---------+---------|
42 * | DEV_OPEN |minor dev| proc nr | O_NOCTTY| | |
43 * |-------------+---------+---------+---------+---------+---------|
44 * | DEV_CLOSE |minor dev| proc nr | | | |
45 * |-------------+---------+---------+---------+---------+---------|
46 * | CANCEL |minor dev| proc nr | | | |
47 * |-------------+---------+---------+---------+---------+---------|
48 * | DEV_SELECT |minor dev| sel ops | | | |
49 * -----------------------------------------------------------------
52 * Jan 20, 2004 moved TTY driver to user-space (Jorrit N. Herder)
53 * Sep 20, 2004 local timer management/ sync alarms (Jorrit N. Herder)
54 * Jul 13, 2004 support for function key observers (Jorrit N. Herder)
58 #include <minix/drivers.h>
59 #include <minix/driver.h>
61 #include <sys/ioc_tty.h>
63 #include <minix/keymap.h>
67 #include <sys/select.h>
69 unsigned long kbd_irq_set
= 0;
70 unsigned long rs_irq_set
= 0;
72 /* Address of a tty structure. */
73 #define tty_addr(line) (&tty_table[line])
75 /* Macros for magic tty types. */
76 #define isconsole(tp) ((tp) < tty_addr(NR_CONS))
77 #define ispty(tp) ((tp) != NULL && (tp)->tty_minor >= TTYPX_MINOR)
79 /* Macros for magic tty structure pointers. */
80 #define FIRST_TTY tty_addr(0)
81 #define END_TTY tty_addr(sizeof(tty_table) / sizeof(tty_table[0]))
83 /* A device exists if at least its 'devread' function is defined. */
84 #define tty_active(tp) ((tp)->tty_devread != NULL)
86 /* RS232 lines or pseudo terminals can be completely configured out. */
88 #define rs_init(tp) ((void) 0)
92 #define pty_init(tp) ((void) 0)
93 #define do_pty(tp, mp) ((void) 0)
96 struct kmessages kmess
;
98 static void tty_timed_out(timer_t
*tp
);
99 static void settimer(tty_t
*tty_ptr
, int enable
);
100 static void in_transfer(tty_t
*tp
);
101 static int tty_echo(tty_t
*tp
, int ch
);
102 static void rawecho(tty_t
*tp
, int ch
);
103 static int back_over(tty_t
*tp
);
104 static void reprint(tty_t
*tp
);
105 static void dev_ioctl(tty_t
*tp
);
106 static void setattr(tty_t
*tp
);
107 static void tty_icancel(tty_t
*tp
);
108 static void tty_init(void);
109 static void do_new_kmess(void);
110 static void set_console_line(char term
[CONS_ARG
]);
111 static void set_kernel_color(char color
[CONS_ARG
]);
112 static void set_color(tty_t
*tp
, int color
);
113 static void reset_color(tty_t
*tp
);
115 static int do_open(devminor_t minor
, int access
, endpoint_t user_endpt
);
116 static int do_close(devminor_t minor
);
117 static ssize_t
do_read(devminor_t minor
, u64_t position
, endpoint_t endpt
,
118 cp_grant_id_t grant
, size_t size
, int flags
, cdev_id_t id
);
119 static ssize_t
do_write(devminor_t minor
, u64_t position
, endpoint_t endpt
,
120 cp_grant_id_t grant
, size_t size
, int flags
, cdev_id_t id
);
121 static int do_ioctl(devminor_t minor
, unsigned long request
, endpoint_t endpt
,
122 cp_grant_id_t grant
, int flags
, endpoint_t user_endpt
, cdev_id_t id
);
123 static int do_cancel(devminor_t minor
, endpoint_t endpt
, cdev_id_t id
);
124 static int do_select(devminor_t minor
, unsigned int ops
, endpoint_t endpt
);
126 static struct chardriver tty_tab
= {
128 .cdr_close
= do_close
,
130 .cdr_write
= do_write
,
131 .cdr_ioctl
= do_ioctl
,
132 .cdr_cancel
= do_cancel
,
133 .cdr_select
= do_select
136 /* Default attributes. */
137 static struct termios termios_defaults
= {
138 TINPUT_DEF
, TOUTPUT_DEF
, TCTRL_DEF
, TLOCAL_DEF
,
140 TEOF_DEF
, TEOL_DEF
, TERASE_DEF
, TINTR_DEF
, TKILL_DEF
, TMIN_DEF
,
141 TQUIT_DEF
, TTIME_DEF
, TSUSP_DEF
, TSTART_DEF
, TSTOP_DEF
,
142 TREPRINT_DEF
, TLNEXT_DEF
, TDISCARD_DEF
,
143 }, TSPEED_DEF
, TSPEED_DEF
,
145 static struct winsize winsize_defaults
; /* = all zeroes */
147 /* Global variables for the TTY task (declared extern in tty.h). */
148 tty_t tty_table
[NR_CONS
+NR_RS_LINES
+NR_PTYS
];
149 int ccurrent
; /* currently active console */
150 struct machine machine
; /* kernel environment variables */
152 u32_t consoleline
= CONS_MINOR
;
153 u32_t kernel_msg_color
= 0;
155 /* SEF functions and variables. */
156 static void sef_local_startup(void);
157 static int sef_cb_init_fresh(int type
, sef_init_info_t
*info
);
158 static void sef_cb_signal_handler(int signo
);
160 extern struct minix_kerninfo
*_minix_kerninfo
;
162 /*===========================================================================*
164 *===========================================================================*/
167 /* Main routine of the terminal task. */
169 message tty_mess
; /* buffer for all incoming messages */
175 /* SEF local startup. */
178 /* Check for and handle any events on any of the ttys. */
179 for (tp
= FIRST_TTY
; tp
< END_TTY
; tp
++) {
180 if (tp
->tty_events
) handle_events(tp
);
183 /* Get a request message. */
184 r
= driver_receive(ANY
, &tty_mess
, &ipc_status
);
186 panic("driver_receive failed with: %d", r
);
188 /* First handle all kernel notification types that the TTY supports.
189 * - An alarm went off, expire all timers and handle the events.
190 * - A hardware interrupt also is an invitation to check for events.
191 * - A new kernel message is available for printing.
192 * - Reset the console on system shutdown.
193 * Then see if this message is different from a normal device driver
194 * request and should be handled separately. These extra functions
195 * do not operate on a device, in constrast to the driver requests.
198 if (is_ipc_notify(ipc_status
)) {
199 switch (_ENDPOINT_P(tty_mess
.m_source
)) {
201 /* run watchdogs of expired timers */
202 expire_timers(tty_mess
.NOTIFY_TIMESTAMP
);
205 /* hardware interrupt notification */
207 /* fetch chars from keyboard */
208 if (tty_mess
.NOTIFY_ARG
& kbd_irq_set
)
209 kbd_interrupt(&tty_mess
);
212 if (tty_mess
.NOTIFY_ARG
& rs_irq_set
)
213 rs_interrupt(&tty_mess
);
215 /* run watchdogs of expired timers */
216 expire_timers(tty_mess
.NOTIFY_TIMESTAMP
);
223 /* done, get new message */
227 switch (tty_mess
.m_type
) {
228 case TTY_FKEY_CONTROL
: /* (un)register a fkey observer */
229 do_fkey_ctl(&tty_mess
);
232 do_kb_inject(&tty_mess
);
234 default: /* should be a driver request */
235 ; /* do nothing; end switch */
238 if (!IS_DEV_RQ(tty_mess
.m_type
)) {
239 chardriver_process(&tty_tab
, &tty_mess
, ipc_status
);
243 /* Only device requests should get to this point.
244 * All requests have a minor device number.
246 line
= tty_mess
.DEVICE
;
247 if (line
== KBD_MINOR
|| line
== KBDAUX_MINOR
) {
248 do_kbd(&tty_mess
, ipc_status
);
250 } else if (line
== VIDEO_MINOR
) {
251 do_video(&tty_mess
, ipc_status
);
253 } else if (line
- PTYPX_MINOR
< NR_PTYS
&&
254 tty_mess
.m_type
!= DEV_IOCTL_S
) {
255 /* Terminals and pseudo terminals belong together. We can only
256 * make a distinction between the two based on position in the
257 * tty_table and not on minor number (i.e., use ispty macro).
258 * Hence this special case.
260 do_pty(&tty_mess
, ipc_status
);
264 /* Execute the requested device driver function. */
265 chardriver_process(&tty_tab
, &tty_mess
, ipc_status
);
272 set_color(tty_t
*tp
, int color
)
277 snprintf(&buf
[1], sizeof(buf
) - 1, "[1;%dm", color
);
278 do_write(tp
->tty_minor
, 0, KERNEL
, (cp_grant_id_t
) buf
, sizeof(buf
),
283 reset_color(tty_t
*tp
)
287 #define SGR_COLOR_RESET 39
289 snprintf(&buf
[1], sizeof(buf
) - 1, "[0;%dm", SGR_COLOR_RESET
);
290 do_write(tp
->tty_minor
, 0, KERNEL
, (cp_grant_id_t
) buf
, sizeof(buf
),
295 line2tty(devminor_t line
)
297 /* Convert a terminal line to tty_table pointer */
301 /* /dev/log goes to /dev/console, and both may be redirected. */
302 if (line
== CONS_MINOR
|| line
== LOG_MINOR
)
305 if (line
== KBD_MINOR
|| line
== KBDAUX_MINOR
|| line
== VIDEO_MINOR
) {
307 } else if ((line
- CONS_MINOR
) < NR_CONS
) {
308 tp
= tty_addr(line
- CONS_MINOR
);
309 } else if ((line
- RS232_MINOR
) < NR_RS_LINES
) {
310 tp
= tty_addr(line
- RS232_MINOR
+ NR_CONS
);
311 } else if ((line
- TTYPX_MINOR
) < NR_PTYS
) {
312 tp
= tty_addr(line
- TTYPX_MINOR
+ NR_CONS
+ NR_RS_LINES
);
313 } else if ((line
- PTYPX_MINOR
) < NR_PTYS
) {
314 tp
= tty_addr(line
- PTYPX_MINOR
+ NR_CONS
+ NR_RS_LINES
);
319 if (tp
!= NULL
&& !tty_active(tp
))
325 /*===========================================================================*
326 * sef_local_startup *
327 *===========================================================================*/
328 static void sef_local_startup()
330 /* Register init callbacks. */
331 sef_setcb_init_fresh(sef_cb_init_fresh
);
332 sef_setcb_init_restart(sef_cb_init_fresh
);
334 /* No live update support for now. */
336 /* Register signal callbacks. */
337 sef_setcb_signal_handler(sef_cb_signal_handler
);
339 /* Let SEF perform startup. */
343 /*===========================================================================*
344 * sef_cb_init_fresh *
345 *===========================================================================*/
346 static int sef_cb_init_fresh(int UNUSED(type
), sef_init_info_t
*UNUSED(info
))
348 /* Initialize the tty driver. */
352 /* Get kernel environment (protected_mode, pc_at and ega are needed). */
353 if (OK
!= (r
=sys_getmachine(&machine
))) {
354 panic("Couldn't obtain kernel environment: %d", r
);
357 if (env_get_param("console", val
, sizeof(val
)) == OK
) {
358 set_console_line(val
);
361 if ((r
= env_get_param("kernelclr", val
, sizeof(val
))) == OK
) {
362 set_kernel_color(val
);
365 /* Initialize the TTY driver. */
368 /* Final one-time keyboard initialization. */
375 set_console_line(char term
[CONS_ARG
])
377 /* Parse 'term' and redirect console output there. */
381 if (!strncmp(term
, "console", CONS_ARG
- 1)) {
382 consoleline
= CONS_MINOR
+0;
385 /* The other console terminals */
386 for (i
= 1; i
< NR_CONS
; i
++) {
388 strlcpy(cons
, "ttyc0", sizeof(cons
));
390 if (!strncmp(term
, cons
,
391 CONS_ARG
< sizeof(cons
) ? CONS_ARG
-1 : sizeof(cons
) - 1))
392 consoleline
= CONS_MINOR
+ i
;
396 assert(NR_RS_LINES
<= 9);/* below assumes this is the case */
397 for (i
= 0; i
< NR_RS_LINES
; i
++) {
399 strlcpy(sercons
, "tty00", sizeof(sercons
));
401 if (!strncmp(term
, sercons
,
402 CONS_ARG
< sizeof(sercons
) ? CONS_ARG
-1:sizeof(sercons
)-1))
403 consoleline
= RS232_MINOR
+ i
;
408 set_kernel_color(char color
[CONS_ARG
])
412 #define SGR_COLOR_START 30
413 #define SGR_COLOR_END 37
415 def_color
= atoi(color
);
416 if ((SGR_COLOR_START
+ def_color
) >= SGR_COLOR_START
&&
417 (SGR_COLOR_START
+ def_color
) <= SGR_COLOR_END
) {
418 kernel_msg_color
= def_color
+ SGR_COLOR_START
;
425 /* Kernel wants to print a new message */
426 struct kmessages
*kmess_ptr
; /* kmessages structure */
427 char kernel_buf_copy
[_KMESS_BUF_SIZE
];
428 static int prev_next
= 0;
429 int next
, bytes
, copy
, restore
= 0;
432 assert(_minix_kerninfo
);
433 kmess_ptr
= _minix_kerninfo
->kmessages
;
435 /* The kernel buffer is circular; print only the new part. Determine
436 * how many new bytes there are with the help of current and
437 * previous 'next' index. This works fine if less than _KMESS_BUF_SIZE
438 * bytes is new data; else we miss % _KMESS_BUF_SIZE here. Obtain
439 * 'next' only once, since we are operating on shared memory here.
440 * Check for size being positive; the buffer might as well be emptied!
442 next
= kmess_ptr
->km_next
;
443 bytes
= ((next
+ _KMESS_BUF_SIZE
) - prev_next
) % _KMESS_BUF_SIZE
;
445 /* Copy from current position toward end of buffer */
446 copy
= MIN(_KMESS_BUF_SIZE
- prev_next
, bytes
);
447 memcpy(kernel_buf_copy
, &kmess_ptr
->km_buf
[prev_next
], copy
);
449 /* Copy remainder from start of buffer */
451 memcpy(&kernel_buf_copy
[copy
], &kmess_ptr
->km_buf
[0],
455 tp
= line2tty(consoleline
);
457 panic("Don't know where to send kernel messages");
458 if (tp
->tty_outleft
> 0) {
459 /* Terminal is already printing */
460 rtp
= *tp
; /* Make backup */
461 tp
->tty_outleft
= 0; /* So do_write is happy */
465 if (kernel_msg_color
!= 0)
466 set_color(tp
, kernel_msg_color
);
467 do_write(tp
->tty_minor
, 0, KERNEL
,
468 (cp_grant_id_t
) kernel_buf_copy
, bytes
,
470 if (kernel_msg_color
!= 0)
477 /* Store 'next' pointer so that we can determine what part of the
478 * kernel messages buffer to print next time a notification arrives.
483 /*===========================================================================*
484 * sef_cb_signal_handler *
485 *===========================================================================*/
486 static void sef_cb_signal_handler(int signo
)
488 /* Check for known signals, ignore anything else. */
490 /* There is a pending message from the kernel. */
494 /* Switch to primary console on termination. */
501 /*===========================================================================*
503 *===========================================================================*/
504 static ssize_t
do_read(devminor_t minor
, u64_t
UNUSED(position
),
505 endpoint_t endpt
, cp_grant_id_t grant
, size_t size
, int flags
,
508 /* A process wants to read from a terminal. */
512 if ((tp
= line2tty(minor
)) == NULL
)
515 /* Check if there is already a process hanging in a read, check if the
516 * parameters are correct, do I/O.
518 if (tp
->tty_incaller
!= NONE
|| tp
->tty_inleft
> 0)
523 /* Copy information from the message to the tty struct. */
524 tp
->tty_incaller
= endpt
;
526 tp
->tty_ingrant
= grant
;
527 assert(tp
->tty_incum
== 0);
528 tp
->tty_inleft
= size
;
530 if (!(tp
->tty_termios
.c_lflag
& ICANON
) && tp
->tty_termios
.c_cc
[VTIME
] > 0) {
531 if (tp
->tty_termios
.c_cc
[VMIN
] == 0) {
532 /* MIN & TIME specify a read timer that finishes the
533 * read in TIME/10 seconds if no bytes are available.
538 /* MIN & TIME specify an inter-byte timer that may
539 * have to be cancelled if there are no bytes yet.
541 if (tp
->tty_eotct
== 0) {
543 tp
->tty_min
= tp
->tty_termios
.c_cc
[VMIN
];
548 /* Anything waiting in the input buffer? Clear it out... */
550 /* ...then go back for more. */
552 if (tp
->tty_inleft
== 0)
553 return EDONTREPLY
; /* already done */
555 /* There were no bytes in the input queue available. */
556 if (flags
& FLG_OP_NONBLOCK
) {
558 r
= tp
->tty_incum
> 0 ? tp
->tty_incum
: EAGAIN
;
559 tp
->tty_inleft
= tp
->tty_incum
= 0;
560 tp
->tty_incaller
= NONE
;
564 if (tp
->tty_select_ops
)
567 return EDONTREPLY
; /* suspend the caller */
570 /*===========================================================================*
572 *===========================================================================*/
573 static ssize_t
do_write(devminor_t minor
, u64_t
UNUSED(position
),
574 endpoint_t endpt
, cp_grant_id_t grant
, size_t size
, int flags
,
577 /* A process wants to write on a terminal. */
581 if ((tp
= line2tty(minor
)) == NULL
)
584 /* Check if there is already a process hanging in a write, check if the
585 * parameters are correct, do I/O.
587 if (tp
->tty_outcaller
!= NONE
|| tp
->tty_outleft
> 0)
592 /* Copy message parameters to the tty structure. */
593 tp
->tty_outcaller
= endpt
;
595 tp
->tty_outgrant
= grant
;
596 assert(tp
->tty_outcum
== 0);
597 tp
->tty_outleft
= size
;
601 if (tp
->tty_outleft
== 0)
602 return EDONTREPLY
; /* already done */
604 /* None or not all the bytes could be written. */
605 if (flags
& FLG_OP_NONBLOCK
) {
606 r
= tp
->tty_outcum
> 0 ? tp
->tty_outcum
: EAGAIN
;
607 tp
->tty_outleft
= tp
->tty_outcum
= 0;
608 tp
->tty_outcaller
= NONE
;
612 if (tp
->tty_select_ops
)
615 return EDONTREPLY
; /* suspend the caller */
618 /*===========================================================================*
620 *===========================================================================*/
621 static int do_ioctl(devminor_t minor
, unsigned long request
, endpoint_t endpt
,
622 cp_grant_id_t grant
, int flags
, endpoint_t user_endpt
, cdev_id_t id
)
624 /* Perform an IOCTL on this terminal. POSIX termios calls are handled
625 * by the IOCTL system call.
631 if ((tp
= line2tty(minor
)) == NULL
)
634 /* Size of the ioctl parameter. */
636 case TCGETS
: /* Posix tcgetattr function */
637 case TCSETS
: /* Posix tcsetattr function, TCSANOW option */
638 case TCSETSW
: /* Posix tcsetattr function, TCSADRAIN option */
639 case TCSETSF
: /* Posix tcsetattr function, TCSAFLUSH option */
640 size
= sizeof(struct termios
);
643 case TCSBRK
: /* Posix tcsendbreak function */
644 case TCFLOW
: /* Posix tcflow function */
645 case TCFLSH
: /* Posix tcflush function */
646 case TIOCGPGRP
: /* Posix tcgetpgrp function */
647 case TIOCSPGRP
: /* Posix tcsetpgrp function */
651 case TIOCGWINSZ
: /* get window size (not Posix) */
652 case TIOCSWINSZ
: /* set window size (not Posix) */
653 size
= sizeof(struct winsize
);
656 case KIOCSMAP
: /* load keymap (Minix extension) */
657 size
= sizeof(keymap_t
);
660 case TIOCSFON
: /* load font (Minix extension) */
661 size
= sizeof(u8_t
[8192]);
664 case TCDRAIN
: /* Posix tcdrain function -- no parameter */
671 /* Get the termios attributes. */
672 r
= sys_safecopyto(endpt
, grant
, 0, (vir_bytes
) &tp
->tty_termios
,
679 if (tp
->tty_outleft
> 0) {
680 if (flags
& FLG_OP_NONBLOCK
)
682 /* Wait for all ongoing output processing to finish. */
683 tp
->tty_iocaller
= endpt
;
685 tp
->tty_ioreq
= request
;
686 tp
->tty_iogrant
= grant
;
687 return EDONTREPLY
; /* suspend the caller */
689 if (request
== TCDRAIN
) break;
690 if (request
== TCSETSF
) tty_icancel(tp
);
693 /* Set the termios attributes. */
694 r
= sys_safecopyfrom(endpt
, grant
, 0, (vir_bytes
) &tp
->tty_termios
,
701 r
= sys_safecopyfrom(endpt
, grant
, 0, (vir_bytes
) &i
, size
);
704 case TCIFLUSH
: tty_icancel(tp
); break;
705 case TCOFLUSH
: (*tp
->tty_ocancel
)(tp
, 0); break;
706 case TCIOFLUSH
: tty_icancel(tp
); (*tp
->tty_ocancel
)(tp
, 0); break;
712 r
= sys_safecopyfrom(endpt
, grant
, 0, (vir_bytes
) &i
, size
);
717 tp
->tty_inhibited
= (i
== TCOOFF
);
721 (*tp
->tty_echo
)(tp
, tp
->tty_termios
.c_cc
[VSTOP
]);
724 (*tp
->tty_echo
)(tp
, tp
->tty_termios
.c_cc
[VSTART
]);
732 if (tp
->tty_break
!= NULL
) (*tp
->tty_break
)(tp
,0);
736 r
= sys_safecopyto(endpt
, grant
, 0, (vir_bytes
) &tp
->tty_winsize
,
741 r
= sys_safecopyfrom(endpt
, grant
, 0, (vir_bytes
) &tp
->tty_winsize
,
743 sigchar(tp
, SIGWINCH
, 0);
747 /* Load a new keymap (only /dev/console). */
748 if (isconsole(tp
)) r
= kbd_loadmap(endpt
, grant
);
752 /* Load a font into an EGA or VGA card (hs@hck.hr) */
753 if (isconsole(tp
)) r
= con_loadfont(endpt
, grant
);
756 /* These Posix functions are allowed to fail if _POSIX_JOB_CONTROL is
768 /*===========================================================================*
770 *===========================================================================*/
771 static int do_open(devminor_t minor
, int access
, endpoint_t user_endpt
)
773 /* A tty line has been opened. Make it the callers controlling tty if
774 * O_NOCTTY is *not* set and it is not the log device. 1 is returned if
775 * the tty is made the controlling tty, otherwise OK or an error code.
780 if ((tp
= line2tty(minor
)) == NULL
)
783 if (minor
== LOG_MINOR
&& isconsole(tp
)) {
784 /* The log device is a write-only diagnostics device. */
785 if (access
& R_BIT
) return EACCES
;
787 if (!(access
& O_NOCTTY
)) {
788 tp
->tty_pgrp
= user_endpt
;
792 if (tp
->tty_openct
== 1) {
793 /* Tell the device that the tty is opened */
794 (*tp
->tty_open
)(tp
, 0);
801 /*===========================================================================*
803 *===========================================================================*/
804 static int do_close(devminor_t minor
)
806 /* A tty line has been closed. Clean up the line if it is the last close. */
809 if ((tp
= line2tty(minor
)) == NULL
)
812 if ((minor
!= LOG_MINOR
|| !isconsole(tp
)) && --tp
->tty_openct
== 0) {
815 (*tp
->tty_ocancel
)(tp
, 0);
816 (*tp
->tty_close
)(tp
, 0);
817 tp
->tty_termios
= termios_defaults
;
818 tp
->tty_winsize
= winsize_defaults
;
825 /*===========================================================================*
827 *===========================================================================*/
828 static int do_cancel(devminor_t minor
, endpoint_t endpt
, cdev_id_t id
)
830 /* A signal has been sent to a process that is hanging trying to read or write.
831 * The pending read or write must be finished off immediately.
836 if ((tp
= line2tty(minor
)) == NULL
)
839 /* Check the parameters carefully, to avoid cancelling twice. */
841 if (tp
->tty_inleft
!= 0 && endpt
== tp
->tty_incaller
&& id
== tp
->tty_inid
) {
842 /* Process was reading when killed. Clean up input. */
844 r
= tp
->tty_incum
> 0 ? tp
->tty_incum
: EAGAIN
;
845 tp
->tty_inleft
= tp
->tty_incum
= 0;
846 tp
->tty_incaller
= NONE
;
847 } else if (tp
->tty_outleft
!= 0 && endpt
== tp
->tty_outcaller
&&
848 id
== tp
->tty_outid
) {
849 /* Process was writing when killed. Clean up output. */
850 r
= tp
->tty_outcum
> 0 ? tp
->tty_outcum
: EAGAIN
;
851 tp
->tty_outleft
= tp
->tty_outcum
= 0;
852 tp
->tty_outcaller
= NONE
;
853 } else if (tp
->tty_ioreq
!= 0 && endpt
== tp
->tty_iocaller
&&
854 id
== tp
->tty_ioid
) {
855 /* Process was waiting for output to drain. */
858 tp
->tty_iocaller
= NONE
;
862 /* Only reply if we found a matching request. */
866 int select_try(struct tty
*tp
, int ops
)
870 /* Special case. If line is hung up, no operations will block.
871 * (and it can be seen as an exceptional condition.)
873 if (tp
->tty_termios
.c_ospeed
== B0
) {
878 /* will i/o not block on read? */
879 if (tp
->tty_inleft
> 0) {
880 ready_ops
|= SEL_RD
; /* EIO - no blocking */
881 } else if (tp
->tty_incount
> 0) {
882 /* Is a regular read possible? tty_incount
883 * says there is data. But a read will only succeed
884 * in canonical mode if a newline has been seen.
886 if (!(tp
->tty_termios
.c_lflag
& ICANON
) ||
894 if (tp
->tty_outleft
> 0) ready_ops
|= SEL_WR
;
895 else if ((*tp
->tty_devwrite
)(tp
, 1)) ready_ops
|= SEL_WR
;
900 int select_retry(struct tty
*tp
)
904 if (tp
->tty_select_ops
&& (ops
= select_try(tp
, tp
->tty_select_ops
))) {
905 chardriver_reply_select(tp
->tty_select_proc
, tp
->tty_minor
,
907 tp
->tty_select_ops
&= ~ops
;
912 /*===========================================================================*
914 *===========================================================================*/
915 static int do_select(devminor_t minor
, unsigned int ops
, endpoint_t endpt
)
918 int ready_ops
, watch
;
920 if ((tp
= line2tty(minor
)) == NULL
)
923 /* Translated minor numbers are a problem when sending late replies. */
924 if (tp
->tty_minor
!= minor
)
927 watch
= (ops
& SEL_NOTIFY
);
928 ops
&= (SEL_RD
| SEL_WR
| SEL_ERR
);
930 ready_ops
= select_try(tp
, ops
);
934 tp
->tty_select_ops
|= ops
;
935 tp
->tty_select_proc
= endpt
;
938 assert(tp
->tty_minor
== minor
);
942 /*===========================================================================*
944 *===========================================================================*/
945 void handle_events(tp
)
946 tty_t
*tp
; /* TTY to check for events. */
948 /* Handle any events pending on a TTY. These events are usually device
951 * Two kinds of events are prominent:
952 * - a character has been received from the console or an RS232 line.
953 * - an RS232 line has completed a write request (on behalf of a user).
954 * The interrupt handler may delay the interrupt message at its discretion
955 * to avoid swamping the TTY task. Messages may be overwritten when the
956 * lines are fast or when there are races between different lines, input
957 * and output, because MINIX only provides single buffering for interrupt
958 * messages. This is handled by explicitly checking each line for fresh input
959 * and completed output on each interrupt.
965 /* Read input and perform input processing. */
966 (*tp
->tty_devread
)(tp
, 0);
968 /* Perform output processing and write output. */
969 (*tp
->tty_devwrite
)(tp
, 0);
971 /* Ioctl waiting for some event? */
972 if (tp
->tty_ioreq
!= 0) dev_ioctl(tp
);
973 } while (tp
->tty_events
);
975 /* Transfer characters from the input queue to a waiting process. */
978 /* Reply if enough bytes are available. */
979 if (tp
->tty_incum
>= tp
->tty_min
&& tp
->tty_inleft
> 0) {
980 chardriver_reply_task(tp
->tty_incaller
, tp
->tty_inid
, tp
->tty_incum
);
981 tp
->tty_inleft
= tp
->tty_incum
= 0;
982 tp
->tty_incaller
= NONE
;
984 if (tp
->tty_select_ops
)
990 select_retry_pty(tp
);
994 /*===========================================================================*
996 *===========================================================================*/
997 static void in_transfer(tp
)
998 register tty_t
*tp
; /* pointer to terminal to read from */
1000 /* Transfer bytes from the input queue to a process reading from a terminal. */
1006 /* Force read to succeed if the line is hung up, looks like EOF to reader. */
1007 if (tp
->tty_termios
.c_ospeed
== B0
) tp
->tty_min
= 0;
1009 /* Anything to do? */
1010 if (tp
->tty_inleft
== 0 || tp
->tty_eotct
< tp
->tty_min
) return;
1013 while (tp
->tty_inleft
> 0 && tp
->tty_eotct
> 0) {
1014 ch
= *tp
->tty_intail
;
1016 if (!(ch
& IN_EOF
)) {
1017 /* One character to be delivered to the user. */
1020 if (++bp
== bufend(buf
)) {
1021 /* Temp buffer full, copy to user space. */
1022 sys_safecopyto(tp
->tty_incaller
,
1023 tp
->tty_ingrant
, tp
->tty_incum
,
1024 (vir_bytes
) buf
, (vir_bytes
) buflen(buf
));
1025 tp
->tty_incum
+= buflen(buf
);
1030 /* Remove the character from the input queue. */
1031 if (++tp
->tty_intail
== bufend(tp
->tty_inbuf
))
1032 tp
->tty_intail
= tp
->tty_inbuf
;
1036 /* Don't read past a line break in canonical mode. */
1037 if (tp
->tty_termios
.c_lflag
& ICANON
) tp
->tty_inleft
= 0;
1042 /* Leftover characters in the buffer. */
1044 sys_safecopyto(tp
->tty_incaller
, tp
->tty_ingrant
, tp
->tty_incum
,
1045 (vir_bytes
) buf
, (vir_bytes
) count
);
1046 tp
->tty_incum
+= count
;
1049 /* Usually reply to the reader, possibly even if incum == 0 (EOF). */
1050 if (tp
->tty_inleft
== 0) {
1051 chardriver_reply_task(tp
->tty_incaller
, tp
->tty_inid
, tp
->tty_incum
);
1052 tp
->tty_inleft
= tp
->tty_incum
= 0;
1053 tp
->tty_incaller
= NONE
;
1057 /*===========================================================================*
1059 *===========================================================================*/
1060 static void in_process_send_byte(
1061 tty_t
*tp
, /* terminal on which character has arrived */
1062 int ch
/* input character */
1065 /* Save the character in the input queue. */
1066 *tp
->tty_inhead
++ = ch
;
1067 if (tp
->tty_inhead
== bufend(tp
->tty_inbuf
))
1068 tp
->tty_inhead
= tp
->tty_inbuf
;
1070 if (ch
& IN_EOT
) tp
->tty_eotct
++;
1072 /* Try to finish input if the queue threatens to overflow. */
1073 if (tp
->tty_incount
== buflen(tp
->tty_inbuf
)) in_transfer(tp
);
1076 int in_process(tp
, buf
, count
, scode
)
1077 register tty_t
*tp
; /* terminal on which character has arrived */
1078 char *buf
; /* buffer with input characters */
1079 int count
; /* number of input characters */
1080 int scode
; /* scan code */
1082 /* Characters have just been typed in. Process, save, and echo them. Return
1083 * the number of characters processed.
1087 int timeset
= FALSE
;
1089 /* Send scancode if requested */
1090 if (tp
->tty_termios
.c_iflag
& SCANCODES
) {
1091 in_process_send_byte(tp
, (scode
& BYTE
) | IN_EOT
);
1094 for (ct
= 0; ct
< count
; ct
++) {
1095 /* Take one character. */
1098 /* Strip to seven bits? */
1099 if (tp
->tty_termios
.c_iflag
& ISTRIP
) ch
&= 0x7F;
1101 /* Input extensions? */
1102 if (tp
->tty_termios
.c_lflag
& IEXTEN
) {
1104 /* Previous character was a character escape? */
1105 if (tp
->tty_escaped
) {
1106 tp
->tty_escaped
= NOT_ESCAPED
;
1107 ch
|= IN_ESC
; /* protect character */
1110 /* LNEXT (^V) to escape the next character? */
1111 if (ch
== tp
->tty_termios
.c_cc
[VLNEXT
]) {
1112 tp
->tty_escaped
= ESCAPED
;
1115 continue; /* do not store the escape */
1118 /* REPRINT (^R) to reprint echoed characters? */
1119 if (ch
== tp
->tty_termios
.c_cc
[VREPRINT
]) {
1125 /* _POSIX_VDISABLE is a normal character value, so better escape it. */
1126 if (ch
== _POSIX_VDISABLE
) ch
|= IN_ESC
;
1128 /* Map CR to LF, ignore CR, or map LF to CR. */
1130 if (tp
->tty_termios
.c_iflag
& IGNCR
) continue;
1131 if (tp
->tty_termios
.c_iflag
& ICRNL
) ch
= '\n';
1134 if (tp
->tty_termios
.c_iflag
& INLCR
) ch
= '\r';
1137 /* Canonical mode? */
1138 if (tp
->tty_termios
.c_lflag
& ICANON
) {
1140 /* Erase processing (rub out of last character). */
1141 if (ch
== tp
->tty_termios
.c_cc
[VERASE
]) {
1142 (void) back_over(tp
);
1143 if (!(tp
->tty_termios
.c_lflag
& ECHOE
)) {
1144 (void) tty_echo(tp
, ch
);
1149 /* Kill processing (remove current line). */
1150 if (ch
== tp
->tty_termios
.c_cc
[VKILL
]) {
1151 while (back_over(tp
)) {}
1152 if (!(tp
->tty_termios
.c_lflag
& ECHOE
)) {
1153 (void) tty_echo(tp
, ch
);
1154 if (tp
->tty_termios
.c_lflag
& ECHOK
)
1160 /* EOF (^D) means end-of-file, an invisible "line break". */
1161 if (ch
== tp
->tty_termios
.c_cc
[VEOF
]) ch
|= IN_EOT
| IN_EOF
;
1163 /* The line may be returned to the user after an LF. */
1164 if (ch
== '\n') ch
|= IN_EOT
;
1166 /* Same thing with EOL, whatever it may be. */
1167 if (ch
== tp
->tty_termios
.c_cc
[VEOL
]) ch
|= IN_EOT
;
1170 /* Start/stop input control? */
1171 if (tp
->tty_termios
.c_iflag
& IXON
) {
1173 /* Output stops on STOP (^S). */
1174 if (ch
== tp
->tty_termios
.c_cc
[VSTOP
]) {
1175 tp
->tty_inhibited
= STOPPED
;
1180 /* Output restarts on START (^Q) or any character if IXANY. */
1181 if (tp
->tty_inhibited
) {
1182 if (ch
== tp
->tty_termios
.c_cc
[VSTART
]
1183 || (tp
->tty_termios
.c_iflag
& IXANY
)) {
1184 tp
->tty_inhibited
= RUNNING
;
1186 if (ch
== tp
->tty_termios
.c_cc
[VSTART
])
1192 if (tp
->tty_termios
.c_lflag
& ISIG
) {
1193 /* Check for INTR (^?) and QUIT (^\) characters. */
1194 if (ch
== tp
->tty_termios
.c_cc
[VINTR
]
1195 || ch
== tp
->tty_termios
.c_cc
[VQUIT
]) {
1197 if (ch
== tp
->tty_termios
.c_cc
[VQUIT
]) sig
= SIGQUIT
;
1198 sigchar(tp
, sig
, 1);
1199 (void) tty_echo(tp
, ch
);
1204 /* Is there space in the input buffer? */
1205 if (tp
->tty_incount
== buflen(tp
->tty_inbuf
)) {
1206 /* No space; discard in canonical mode, keep in raw mode. */
1207 if (tp
->tty_termios
.c_lflag
& ICANON
) continue;
1211 if (!(tp
->tty_termios
.c_lflag
& ICANON
)) {
1212 /* In raw mode all characters are "line breaks". */
1215 /* Start an inter-byte timer? */
1216 if (!timeset
&& tp
->tty_termios
.c_cc
[VMIN
] > 0
1217 && tp
->tty_termios
.c_cc
[VTIME
] > 0) {
1223 /* Perform the intricate function of echoing. */
1224 if (tp
->tty_termios
.c_lflag
& (ECHO
|ECHONL
)) ch
= tty_echo(tp
, ch
);
1226 /* Send processed byte of input unless scancodes sent instead */
1227 if (!(tp
->tty_termios
.c_iflag
& SCANCODES
)) {
1228 in_process_send_byte(tp
, ch
);
1234 /*===========================================================================*
1236 *===========================================================================*/
1237 static int tty_echo(tp
, ch
)
1238 register tty_t
*tp
; /* terminal on which to echo */
1239 register int ch
; /* pointer to character to echo */
1241 /* Echo the character if echoing is on. Some control characters are echoed
1242 * with their normal effect, other control characters are echoed as "^X",
1243 * normal characters are echoed normally. EOF (^D) is echoed, but immediately
1244 * backspaced over. Return the character with the echoed length added to its
1250 if (!(tp
->tty_termios
.c_lflag
& ECHO
)) {
1251 if (ch
== ('\n' | IN_EOT
) && (tp
->tty_termios
.c_lflag
1252 & (ICANON
|ECHONL
)) == (ICANON
|ECHONL
))
1253 (*tp
->tty_echo
)(tp
, '\n');
1257 /* "Reprint" tells if the echo output has been messed up by other output. */
1258 rp
= tp
->tty_incount
== 0 ? FALSE
: tp
->tty_reprint
;
1260 if ((ch
& IN_CHAR
) < ' ') {
1261 switch (ch
& (IN_ESC
|IN_EOF
|IN_EOT
|IN_CHAR
)) {
1265 (*tp
->tty_echo
)(tp
, ' ');
1267 } while (len
< TAB_SIZE
&& (tp
->tty_position
& TAB_MASK
) != 0);
1271 (*tp
->tty_echo
)(tp
, ch
& IN_CHAR
);
1275 (*tp
->tty_echo
)(tp
, '^');
1276 (*tp
->tty_echo
)(tp
, '@' + (ch
& IN_CHAR
));
1280 if ((ch
& IN_CHAR
) == '\177') {
1281 /* A DEL prints as "^?". */
1282 (*tp
->tty_echo
)(tp
, '^');
1283 (*tp
->tty_echo
)(tp
, '?');
1286 (*tp
->tty_echo
)(tp
, ch
& IN_CHAR
);
1289 if (ch
& IN_EOF
) while (len
> 0) { (*tp
->tty_echo
)(tp
, '\b'); len
--; }
1291 tp
->tty_reprint
= rp
;
1292 return(ch
| (len
<< IN_LSHIFT
));
1295 /*===========================================================================*
1297 *===========================================================================*/
1298 static void rawecho(tp
, ch
)
1302 /* Echo without interpretation if ECHO is set. */
1303 int rp
= tp
->tty_reprint
;
1304 if (tp
->tty_termios
.c_lflag
& ECHO
) (*tp
->tty_echo
)(tp
, ch
);
1305 tp
->tty_reprint
= rp
;
1308 /*===========================================================================*
1310 *===========================================================================*/
1311 static int back_over(tp
)
1314 /* Backspace to previous character on screen and erase it. */
1318 if (tp
->tty_incount
== 0) return(0); /* queue empty */
1319 head
= tp
->tty_inhead
;
1320 if (head
== tp
->tty_inbuf
) head
= bufend(tp
->tty_inbuf
);
1321 if (*--head
& IN_EOT
) return(0); /* can't erase "line breaks" */
1322 if (tp
->tty_reprint
) reprint(tp
); /* reprint if messed up */
1323 tp
->tty_inhead
= head
;
1325 if (tp
->tty_termios
.c_lflag
& ECHOE
) {
1326 len
= (*head
& IN_LEN
) >> IN_LSHIFT
;
1334 return(1); /* one character erased */
1337 /*===========================================================================*
1339 *===========================================================================*/
1340 static void reprint(tp
)
1341 register tty_t
*tp
; /* pointer to tty struct */
1343 /* Restore what has been echoed to screen before if the user input has been
1344 * messed up by output, or if REPRINT (^R) is typed.
1349 tp
->tty_reprint
= FALSE
;
1351 /* Find the last line break in the input. */
1352 head
= tp
->tty_inhead
;
1353 count
= tp
->tty_incount
;
1355 if (head
== tp
->tty_inbuf
) head
= bufend(tp
->tty_inbuf
);
1356 if (head
[-1] & IN_EOT
) break;
1360 if (count
== tp
->tty_incount
) return; /* no reason to reprint */
1362 /* Show REPRINT (^R) and move to a new line. */
1363 (void) tty_echo(tp
, tp
->tty_termios
.c_cc
[VREPRINT
] | IN_ESC
);
1367 /* Reprint from the last break onwards. */
1369 if (head
== bufend(tp
->tty_inbuf
)) head
= tp
->tty_inbuf
;
1370 *head
= tty_echo(tp
, *head
);
1373 } while (count
< tp
->tty_incount
);
1376 /*===========================================================================*
1378 *===========================================================================*/
1379 void out_process(tp
, bstart
, bpos
, bend
, icount
, ocount
)
1381 char *bstart
, *bpos
, *bend
; /* start/pos/end of circular buffer */
1382 int *icount
; /* # input chars / input chars used */
1383 int *ocount
; /* max output chars / output chars used */
1385 /* Perform output processing on a circular buffer. *icount is the number of
1386 * bytes to process, and the number of bytes actually processed on return.
1387 * *ocount is the space available on input and the space used on output.
1388 * (Naturally *icount < *ocount.) The column position is updated modulo
1389 * the TAB size, because we really only need it for tabs.
1395 int pos
= tp
->tty_position
;
1408 if ((tp
->tty_termios
.c_oflag
& (OPOST
|ONLCR
))
1410 /* Map LF to CR+LF if there is space. Note that the
1411 * next character in the buffer is overwritten, so
1412 * we stop at this point.
1416 if (++bpos
== bend
) bpos
= bstart
;
1422 goto out_done
; /* no space or buffer got changed */
1426 /* Best guess for the tab length. */
1427 tablen
= TAB_SIZE
- (pos
& TAB_MASK
);
1429 if ((tp
->tty_termios
.c_oflag
& (OPOST
|XTABS
))
1431 /* Tabs must be expanded. */
1432 if (oct
>= tablen
) {
1438 if (++bpos
== bend
) bpos
= bstart
;
1439 } while (--tablen
!= 0);
1443 /* Tabs are output directly. */
1447 /* Assume any other character prints as one character. */
1450 if (++bpos
== bend
) bpos
= bstart
;
1455 tp
->tty_position
= pos
& TAB_MASK
;
1457 *icount
-= ict
; /* [io]ct are the number of chars not used */
1458 *ocount
-= oct
; /* *[io]count are the number of chars that are used */
1461 /*===========================================================================*
1463 *===========================================================================*/
1464 static void dev_ioctl(tp
)
1467 /* The ioctl's TCSETSW, TCSETSF and TCDRAIN wait for output to finish to make
1468 * sure that an attribute change doesn't affect the processing of current
1469 * output. Once output finishes the ioctl is executed as in do_ioctl().
1471 int result
= EINVAL
;
1473 if (tp
->tty_outleft
> 0) return; /* output not finished */
1475 if (tp
->tty_ioreq
!= TCDRAIN
) {
1476 if (tp
->tty_ioreq
== TCSETSF
) tty_icancel(tp
);
1477 result
= sys_safecopyfrom(tp
->tty_iocaller
, tp
->tty_iogrant
, 0,
1478 (vir_bytes
) &tp
->tty_termios
,
1479 (vir_bytes
) sizeof(tp
->tty_termios
));
1480 if (result
== OK
) setattr(tp
);
1483 chardriver_reply_task(tp
->tty_iocaller
, tp
->tty_ioid
, result
);
1484 tp
->tty_iocaller
= NONE
;
1487 /*===========================================================================*
1489 *===========================================================================*/
1490 static void setattr(tp
)
1493 /* Apply the new line attributes (raw/canonical, line speed, etc.) */
1497 if (!(tp
->tty_termios
.c_lflag
& ICANON
)) {
1498 /* Raw mode; put a "line break" on all characters in the input queue.
1499 * It is undefined what happens to the input queue when ICANON is
1500 * switched off, a process should use TCSAFLUSH to flush the queue.
1501 * Keeping the queue to preserve typeahead is the Right Thing, however
1502 * when a process does use TCSANOW to switch to raw mode.
1504 count
= tp
->tty_eotct
= tp
->tty_incount
;
1505 inp
= tp
->tty_intail
;
1508 if (++inp
== bufend(tp
->tty_inbuf
)) inp
= tp
->tty_inbuf
;
1513 /* Inspect MIN and TIME. */
1514 settimer(tp
, FALSE
);
1515 if (tp
->tty_termios
.c_lflag
& ICANON
) {
1516 /* No MIN & TIME in canonical mode. */
1519 /* In raw mode MIN is the number of chars wanted, and TIME how long
1520 * to wait for them. With interesting exceptions if either is zero.
1522 tp
->tty_min
= tp
->tty_termios
.c_cc
[VMIN
];
1523 if (tp
->tty_min
== 0 && tp
->tty_termios
.c_cc
[VTIME
] > 0)
1527 if (!(tp
->tty_termios
.c_iflag
& IXON
)) {
1528 /* No start/stop output control, so don't leave output inhibited. */
1529 tp
->tty_inhibited
= RUNNING
;
1533 /* Setting the output speed to zero hangs up the phone. */
1534 if (tp
->tty_termios
.c_ospeed
== B0
) sigchar(tp
, SIGHUP
, 1);
1536 /* SCANCODES is supported only for the console */
1537 if (!isconsole(tp
)) tp
->tty_termios
.c_iflag
&= ~SCANCODES
;
1539 /* Set new line speed, character size, etc at the device level. */
1540 (*tp
->tty_ioctl
)(tp
, 0);
1543 /*===========================================================================*
1545 *===========================================================================*/
1546 void sigchar(tp
, sig
, mayflush
)
1548 int sig
; /* SIGINT, SIGQUIT, SIGKILL or SIGHUP */
1551 /* Process a SIGINT, SIGQUIT or SIGKILL char from the keyboard or SIGHUP from
1552 * a tty close, "stty 0", or a real RS-232 hangup. PM will send the signal to
1553 * the process group (INT, QUIT), all processes (KILL), or the session leader
1558 if (tp
->tty_pgrp
!= 0) {
1559 if (OK
!= (status
= sys_kill(tp
->tty_pgrp
, sig
))) {
1560 panic("Error; call to sys_kill failed: %d", status
);
1564 if (mayflush
&& !(tp
->tty_termios
.c_lflag
& NOFLSH
)) {
1565 tp
->tty_incount
= tp
->tty_eotct
= 0; /* kill earlier input */
1566 tp
->tty_intail
= tp
->tty_inhead
;
1567 (*tp
->tty_ocancel
)(tp
, 0); /* kill all output */
1568 tp
->tty_inhibited
= RUNNING
;
1573 /*===========================================================================*
1575 *===========================================================================*/
1576 static void tty_icancel(tp
)
1579 /* Discard all pending input, tty buffer or device. */
1581 tp
->tty_incount
= tp
->tty_eotct
= 0;
1582 tp
->tty_intail
= tp
->tty_inhead
;
1583 (*tp
->tty_icancel
)(tp
, 0);
1586 /*===========================================================================*
1588 *===========================================================================*/
1589 static int tty_devnop(tty_t
*UNUSED(tp
), int UNUSED(try))
1591 /* Some functions need not be implemented at the device level. */
1595 /*===========================================================================*
1597 *===========================================================================*/
1598 static void tty_init()
1600 /* Initialize tty structure and call device initialization routines. */
1605 system_hz
= sys_hz();
1607 /* Initialize the terminal lines. */
1608 memset(tty_table
, '\0' , sizeof(tty_table
));
1610 for (tp
= FIRST_TTY
,s
=0; tp
< END_TTY
; tp
++,s
++) {
1613 init_timer(&tp
->tty_tmr
);
1615 tp
->tty_intail
= tp
->tty_inhead
= tp
->tty_inbuf
;
1617 tp
->tty_incaller
= tp
->tty_outcaller
= tp
->tty_iocaller
= NONE
;
1618 tp
->tty_termios
= termios_defaults
;
1619 tp
->tty_icancel
= tp
->tty_ocancel
= tp
->tty_ioctl
= tp
->tty_close
=
1620 tp
->tty_open
= tty_devnop
;
1621 if (tp
< tty_addr(NR_CONS
)) {
1624 /* Initialize the keyboard driver. */
1627 tp
->tty_minor
= CONS_MINOR
+ s
;
1629 if (tp
< tty_addr(NR_CONS
+NR_RS_LINES
)) {
1631 tp
->tty_minor
= RS232_MINOR
+ s
-NR_CONS
;
1634 tp
->tty_minor
= s
- (NR_CONS
+NR_RS_LINES
) + TTYPX_MINOR
;
1640 /*===========================================================================*
1642 *===========================================================================*/
1643 static void tty_timed_out(timer_t
*tp
)
1645 /* This timer has expired. Set the events flag, to force processing. */
1647 tty_ptr
= &tty_table
[tmr_arg(tp
)->ta_int
];
1648 tty_ptr
->tty_min
= 0; /* force read to succeed */
1649 tty_ptr
->tty_events
= 1;
1652 /*===========================================================================*
1654 *===========================================================================*/
1655 static void settimer(tty_ptr
, enable
)
1656 tty_t
*tty_ptr
; /* line to set or unset a timer on */
1657 int enable
; /* set timer if true, otherwise unset */
1662 ticks
= tty_ptr
->tty_termios
.c_cc
[VTIME
] * (system_hz
/10);
1664 /* Set a new timer for enabling the TTY events flags. */
1665 set_timer(&tty_ptr
->tty_tmr
, ticks
, tty_timed_out
, tty_ptr
->tty_index
);
1667 /* Remove the timer from the active and expired lists. */
1668 cancel_timer(&tty_ptr
->tty_tmr
);