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 * HARD_INT: output has been completed or input has arrived
22 * SYS_SIG: e.g., MINIX wants to shutdown; run code to cleanly stop
23 * DEV_READ: a process wants to read from a terminal
24 * DEV_WRITE: a process wants to write on a terminal
25 * DEV_IOCTL: a process wants to change a terminal's parameters
26 * DEV_OPEN: a tty line has been opened
27 * DEV_CLOSE: a tty line has been closed
28 * DEV_SELECT: start select notification request
29 * DEV_STATUS: FS wants to know status for SELECT or REVIVE
30 * CANCEL: terminate a previous incomplete system call immediately
32 * m_type TTY_LINE IO_ENDPT COUNT TTY_SPEKS ADDRESS
33 * -----------------------------------------------------------------
34 * | HARD_INT | | | | | |
35 * |-------------+---------+---------+---------+---------+---------|
36 * | SYS_SIG | sig set | | | | |
37 * |-------------+---------+---------+---------+---------+---------|
38 * | DEV_READ |minor dev| proc nr | count | | buf ptr |
39 * |-------------+---------+---------+---------+---------+---------|
40 * | DEV_WRITE |minor dev| proc nr | count | | buf ptr |
41 * |-------------+---------+---------+---------+---------+---------|
42 * | DEV_IOCTL |minor dev| proc nr |func code|erase etc| |
43 * |-------------+---------+---------+---------+---------+---------|
44 * | DEV_OPEN |minor dev| proc nr | O_NOCTTY| | |
45 * |-------------+---------+---------+---------+---------+---------|
46 * | DEV_CLOSE |minor dev| proc nr | | | |
47 * |-------------+---------+---------+---------+---------+---------|
48 * | DEV_STATUS | | | | | |
49 * |-------------+---------+---------+---------+---------+---------|
50 * | CANCEL |minor dev| proc nr | | | |
51 * -----------------------------------------------------------------
54 * Jan 20, 2004 moved TTY driver to user-space (Jorrit N. Herder)
55 * Sep 20, 2004 local timer management/ sync alarms (Jorrit N. Herder)
56 * Jul 13, 2004 support for function key observers (Jorrit N. Herder)
59 #include "../drivers.h"
61 #include <sys/ioc_tty.h>
63 #include <minix/callnr.h>
65 #include <minix/keymap.h>
70 #include <sys/select.h>
72 extern int irq_hook_id
;
74 unsigned long kbd_irq_set
= 0;
75 unsigned long rs_irq_set
= 0;
77 /* Address of a tty structure. */
78 #define tty_addr(line) (&tty_table[line])
80 /* Macros for magic tty types. */
81 #define isconsole(tp) ((tp) < tty_addr(NR_CONS))
82 #define ispty(tp) ((tp) >= tty_addr(NR_CONS+NR_RS_LINES))
84 /* Macros for magic tty structure pointers. */
85 #define FIRST_TTY tty_addr(0)
86 #define END_TTY tty_addr(sizeof(tty_table) / sizeof(tty_table[0]))
88 /* A device exists if at least its 'devread' function is defined. */
89 #define tty_active(tp) ((tp)->tty_devread != NULL)
91 /* RS232 lines or pseudo terminals can be completely configured out. */
93 #define rs_init(tp) ((void) 0)
96 #define pty_init(tp) ((void) 0)
97 #define do_pty(tp, mp) ((void) 0)
100 struct kmessages kmess
;
102 FORWARD
_PROTOTYPE( void tty_timed_out
, (timer_t
*tp
) );
103 FORWARD
_PROTOTYPE( void expire_timers
, (void) );
104 FORWARD
_PROTOTYPE( void settimer
, (tty_t
*tty_ptr
, int enable
) );
105 FORWARD
_PROTOTYPE( void do_cancel
, (tty_t
*tp
, message
*m_ptr
) );
106 FORWARD
_PROTOTYPE( void do_ioctl
, (tty_t
*tp
, message
*m_ptr
, int s
) );
107 FORWARD
_PROTOTYPE( void do_open
, (tty_t
*tp
, message
*m_ptr
) );
108 FORWARD
_PROTOTYPE( void do_close
, (tty_t
*tp
, message
*m_ptr
) );
109 FORWARD
_PROTOTYPE( void do_read
, (tty_t
*tp
, message
*m_ptr
, int s
) );
110 FORWARD
_PROTOTYPE( void do_write
, (tty_t
*tp
, message
*m_ptr
, int s
) );
111 FORWARD
_PROTOTYPE( void do_select
, (tty_t
*tp
, message
*m_ptr
) );
112 FORWARD
_PROTOTYPE( void do_status
, (message
*m_ptr
) );
113 FORWARD
_PROTOTYPE( void in_transfer
, (tty_t
*tp
) );
114 FORWARD
_PROTOTYPE( int tty_echo
, (tty_t
*tp
, int ch
) );
115 FORWARD
_PROTOTYPE( void rawecho
, (tty_t
*tp
, int ch
) );
116 FORWARD
_PROTOTYPE( int back_over
, (tty_t
*tp
) );
117 FORWARD
_PROTOTYPE( void reprint
, (tty_t
*tp
) );
118 FORWARD
_PROTOTYPE( void dev_ioctl
, (tty_t
*tp
) );
119 FORWARD
_PROTOTYPE( void setattr
, (tty_t
*tp
) );
120 FORWARD
_PROTOTYPE( void tty_icancel
, (tty_t
*tp
) );
121 FORWARD
_PROTOTYPE( void tty_init
, (void) );
123 /* Default attributes. */
124 PRIVATE
struct termios termios_defaults
= {
125 TINPUT_DEF
, TOUTPUT_DEF
, TCTRL_DEF
, TLOCAL_DEF
, TSPEED_DEF
, TSPEED_DEF
,
127 TEOF_DEF
, TEOL_DEF
, TERASE_DEF
, TINTR_DEF
, TKILL_DEF
, TMIN_DEF
,
128 TQUIT_DEF
, TTIME_DEF
, TSUSP_DEF
, TSTART_DEF
, TSTOP_DEF
,
129 TREPRINT_DEF
, TLNEXT_DEF
, TDISCARD_DEF
,
132 PRIVATE
struct winsize winsize_defaults
; /* = all zeroes */
134 /* Global variables for the TTY task (declared extern in tty.h). */
135 PUBLIC tty_t tty_table
[NR_CONS
+NR_RS_LINES
+NR_PTYS
];
136 PUBLIC
int ccurrent
; /* currently active console */
137 PUBLIC timer_t
*tty_timers
; /* queue of TTY timers */
138 PUBLIC
clock_t tty_next_timeout
; /* time that the next alarm is due */
139 PUBLIC
struct machine machine
; /* kernel environment variables */
141 /*===========================================================================*
143 *===========================================================================*/
144 PUBLIC
void main(void)
146 /* Main routine of the terminal task. */
148 message tty_mess
; /* buffer for all incoming messages */
151 register struct proc
*rp
;
154 /* Get kernel environment (protected_mode, pc_at and ega are needed). */
155 if (OK
!= (s
=sys_getmachine(&machine
))) {
156 panic("TTY","Couldn't obtain kernel environment.", s
);
159 /* Initialize the TTY driver. */
162 /* Final one-time keyboard initialization. */
169 /* Check for and handle any events on any of the ttys. */
170 for (tp
= FIRST_TTY
; tp
< END_TTY
; tp
++) {
171 if (tp
->tty_events
) handle_events(tp
);
174 /* Get a request message. */
175 r
= receive(ANY
, &tty_mess
);
177 panic("TTY", "receive failed with %d", r
);
179 /* First handle all kernel notification types that the TTY supports.
180 * - An alarm went off, expire all timers and handle the events.
181 * - A hardware interrupt also is an invitation to check for events.
182 * - A new kernel message is available for printing.
183 * - Reset the console on system shutdown.
184 * Then see if this message is different from a normal device driver
185 * request and should be handled separately. These extra functions
186 * do not operate on a device, in constrast to the driver requests.
188 switch (tty_mess
.m_type
) {
189 case SYN_ALARM
: /* fall through */
190 expire_timers(); /* run watchdogs of expired timers */
191 continue; /* contine to check for events */
193 notify(tty_mess
.m_source
);
195 case HARD_INT
: { /* hardware interrupt notification */
196 if (tty_mess
.NOTIFY_ARG
& kbd_irq_set
)
197 kbd_interrupt(&tty_mess
);/* fetch chars from keyboard */
199 if (tty_mess
.NOTIFY_ARG
& rs_irq_set
)
200 rs_interrupt(&tty_mess
);/* serial I/O */
202 expire_timers(); /* run watchdogs of expired timers */
203 continue; /* contine to check for events */
206 cons_stop(); /* switch to primary console */
209 case SYS_SIG
: { /* system signal */
210 sigset_t sigset
= (sigset_t
) tty_mess
.NOTIFY_ARG
;
211 if (sigismember(&sigset
, SIGKMESS
)) do_new_kmess(&tty_mess
);
214 case DIAGNOSTICS
: /* a server wants to print some */
215 printf("WARNING: old DIAGNOSTICS from %d\n", tty_mess
.m_source
);
216 do_diagnostics(&tty_mess
, 0);
219 do_diagnostics(&tty_mess
, 1);
222 do_get_kmess(&tty_mess
);
225 do_get_kmess_s(&tty_mess
);
227 case FKEY_CONTROL
: /* (un)register a fkey observer */
228 do_fkey_ctl(&tty_mess
);
230 default: /* should be a driver request */
231 ; /* do nothing; end switch */
234 /* Only device requests should get to this point. All requests,
235 * except DEV_STATUS, have a minor device number. Check this
236 * exception and get the minor device number otherwise.
238 if (tty_mess
.m_type
== DEV_STATUS
) {
239 do_status(&tty_mess
);
242 line
= tty_mess
.TTY_LINE
;
243 if (line
== KBD_MINOR
) {
246 } else if (line
== KBDAUX_MINOR
) {
247 do_kbdaux(&tty_mess
);
249 } else if (line
== VIDEO_MINOR
) {
252 } else if ((line
- CONS_MINOR
) < NR_CONS
) {
253 tp
= tty_addr(line
- CONS_MINOR
);
254 } else if (line
== LOG_MINOR
) {
256 } else if ((line
- RS232_MINOR
) < NR_RS_LINES
) {
257 tp
= tty_addr(line
- RS232_MINOR
+ NR_CONS
);
258 } else if ((line
- TTYPX_MINOR
) < NR_PTYS
) {
259 tp
= tty_addr(line
- TTYPX_MINOR
+ NR_CONS
+ NR_RS_LINES
);
260 } else if ((line
- PTYPX_MINOR
) < NR_PTYS
) {
261 tp
= tty_addr(line
- PTYPX_MINOR
+ NR_CONS
+ NR_RS_LINES
);
262 if (tty_mess
.m_type
!= DEV_IOCTL_S
) {
263 do_pty(tp
, &tty_mess
);
270 /* If the device doesn't exist or is not configured return ENXIO. */
271 if (tp
== NULL
|| ! tty_active(tp
)) {
272 printf("Warning, TTY got illegal request %d from %d\n",
273 tty_mess
.m_type
, tty_mess
.m_source
);
274 if (tty_mess
.m_source
!= LOG_PROC_NR
)
276 tty_reply(TASK_REPLY
, tty_mess
.m_source
,
277 tty_mess
.IO_ENDPT
, ENXIO
);
282 /* Execute the requested device driver function. */
283 switch (tty_mess
.m_type
) {
284 case DEV_READ_S
: do_read(tp
, &tty_mess
, 1); break;
285 case DEV_WRITE_S
: do_write(tp
, &tty_mess
, 1); break;
286 case DEV_IOCTL_S
: do_ioctl(tp
, &tty_mess
, 1); break;
287 case DEV_OPEN
: do_open(tp
, &tty_mess
); break;
288 case DEV_CLOSE
: do_close(tp
, &tty_mess
); break;
289 case DEV_SELECT
: do_select(tp
, &tty_mess
); break;
290 case CANCEL
: do_cancel(tp
, &tty_mess
); break;
292 printf("Warning, TTY got unexpected request %d from %d\n",
293 tty_mess
.m_type
, tty_mess
.m_source
);
294 tty_reply(TASK_REPLY
, tty_mess
.m_source
,
295 tty_mess
.IO_ENDPT
, EINVAL
);
300 /*===========================================================================*
302 *===========================================================================*/
303 PRIVATE
void do_status(m_ptr
)
306 register struct tty
*tp
;
311 /* Check for select or revive events on any of the ttys. If we found an,
312 * event return a single status message for it. The FS will make another
313 * call to see if there is more.
316 for (tp
= FIRST_TTY
; tp
< END_TTY
; tp
++) {
317 if ((ops
= select_try(tp
, tp
->tty_select_ops
)) &&
318 tp
->tty_select_proc
== m_ptr
->m_source
) {
320 /* I/O for a selected minor device is ready. */
321 m_ptr
->m_type
= DEV_IO_READY
;
322 m_ptr
->DEV_MINOR
= tp
->tty_minor
;
323 m_ptr
->DEV_SEL_OPS
= ops
;
325 tp
->tty_select_ops
&= ~ops
; /* unmark select event */
329 else if (tp
->tty_inrevived
&& tp
->tty_incaller
== m_ptr
->m_source
) {
331 /* Suspended request finished. Send a REVIVE. */
332 m_ptr
->m_type
= DEV_REVIVE
;
333 m_ptr
->REP_ENDPT
= tp
->tty_inproc
;
334 m_ptr
->REP_IO_GRANT
= tp
->tty_in_vir_g
;
335 m_ptr
->REP_STATUS
= tp
->tty_incum
;
337 tp
->tty_inleft
= tp
->tty_incum
= 0;
338 tp
->tty_inrevived
= 0; /* unmark revive event */
342 else if (tp
->tty_outrevived
&& tp
->tty_outcaller
== m_ptr
->m_source
) {
344 /* Suspended request finished. Send a REVIVE. */
345 m_ptr
->m_type
= DEV_REVIVE
;
346 m_ptr
->REP_ENDPT
= tp
->tty_outproc
;
347 m_ptr
->REP_IO_GRANT
= tp
->tty_out_vir_g
;
348 m_ptr
->REP_STATUS
= tp
->tty_outcum
;
351 tp
->tty_outrevived
= 0; /* unmark revive event */
355 else if (tp
->tty_iorevived
&& tp
->tty_iocaller
== m_ptr
->m_source
) {
356 /* Suspended request finished. Send a REVIVE. */
357 m_ptr
->m_type
= DEV_REVIVE
;
358 m_ptr
->REP_ENDPT
= tp
->tty_ioproc
;
359 m_ptr
->REP_IO_GRANT
= tp
->tty_iovir_g
;
360 m_ptr
->REP_STATUS
= tp
->tty_iostatus
;
361 tp
->tty_iorevived
= 0; /* unmark revive event */
369 event_found
= pty_status(m_ptr
);
372 event_found
= kbd_status(m_ptr
);
375 /* No events of interest were found. Return an empty message. */
376 m_ptr
->m_type
= DEV_NO_STATUS
;
379 /* Almost done. Send back the reply message to the caller. */
380 if ((status
= send(m_ptr
->m_source
, m_ptr
)) != OK
) {
381 panic("TTY","send in do_status failed, status\n", status
);
385 /*===========================================================================*
387 *===========================================================================*/
388 PRIVATE
void do_read(tp
, m_ptr
, safe
)
389 register tty_t
*tp
; /* pointer to tty struct */
390 register message
*m_ptr
; /* pointer to message sent to the task */
391 int safe
; /* use safecopies? */
393 /* A process wants to read from a terminal. */
396 /* Check if there is already a process hanging in a read, check if the
397 * parameters are correct, do I/O.
399 if (tp
->tty_inleft
> 0) {
402 if (m_ptr
->COUNT
<= 0) {
405 /* Copy information from the message to the tty struct. */
406 tp
->tty_inrepcode
= TASK_REPLY
;
407 tp
->tty_incaller
= m_ptr
->m_source
;
408 tp
->tty_inproc
= m_ptr
->IO_ENDPT
;
409 tp
->tty_in_vir_g
= (vir_bytes
) m_ptr
->ADDRESS
;
410 tp
->tty_in_vir_offset
= 0;
411 tp
->tty_in_safe
= safe
;
412 tp
->tty_inleft
= m_ptr
->COUNT
;
414 if (!(tp
->tty_termios
.c_lflag
& ICANON
)
415 && tp
->tty_termios
.c_cc
[VTIME
] > 0) {
416 if (tp
->tty_termios
.c_cc
[VMIN
] == 0) {
417 /* MIN & TIME specify a read timer that finishes the
418 * read in TIME/10 seconds if no bytes are available.
423 /* MIN & TIME specify an inter-byte timer that may
424 * have to be cancelled if there are no bytes yet.
426 if (tp
->tty_eotct
== 0) {
428 tp
->tty_min
= tp
->tty_termios
.c_cc
[VMIN
];
433 /* Anything waiting in the input buffer? Clear it out... */
435 /* ...then go back for more. */
437 if (tp
->tty_inleft
== 0) {
438 if (tp
->tty_select_ops
)
440 return; /* already done */
443 /* There were no bytes in the input queue available, so suspend
446 r
= SUSPEND
; /* suspend the caller */
447 tp
->tty_inrepcode
= TTY_REVIVE
;
449 tty_reply(TASK_REPLY
, m_ptr
->m_source
, m_ptr
->IO_ENDPT
, r
);
450 if (tp
->tty_select_ops
)
454 /*===========================================================================*
456 *===========================================================================*/
457 PRIVATE
void do_write(tp
, m_ptr
, safe
)
459 register message
*m_ptr
; /* pointer to message sent to the task */
462 /* A process wants to write on a terminal. */
465 /* Check if there is already a process hanging in a write, check if the
466 * parameters are correct, do I/O.
468 if (tp
->tty_outleft
> 0) {
471 if (m_ptr
->COUNT
<= 0) {
474 /* Copy message parameters to the tty structure. */
475 tp
->tty_outrepcode
= TASK_REPLY
;
476 tp
->tty_outcaller
= m_ptr
->m_source
;
477 tp
->tty_outproc
= m_ptr
->IO_ENDPT
;
478 tp
->tty_out_vir_g
= (vir_bytes
) m_ptr
->ADDRESS
;
479 tp
->tty_out_vir_offset
= 0;
480 tp
->tty_out_safe
= safe
;
481 tp
->tty_outleft
= m_ptr
->COUNT
;
485 if (tp
->tty_outleft
== 0)
486 return; /* already done */
488 /* None or not all the bytes could be written, so suspend the
491 r
= SUSPEND
; /* suspend the caller */
492 tp
->tty_outrepcode
= TTY_REVIVE
;
494 tty_reply(TASK_REPLY
, m_ptr
->m_source
, m_ptr
->IO_ENDPT
, r
);
497 /*===========================================================================*
499 *===========================================================================*/
500 PRIVATE
void do_ioctl(tp
, m_ptr
, safe
)
502 message
*m_ptr
; /* pointer to message sent to task */
505 /* Perform an IOCTL on this terminal. Posix termios calls are handled
506 * by the IOCTL system call
515 /* Size of the ioctl parameter. */
516 switch (m_ptr
->TTY_REQUEST
) {
517 case TCGETS
: /* Posix tcgetattr function */
518 case TCSETS
: /* Posix tcsetattr function, TCSANOW option */
519 case TCSETSW
: /* Posix tcsetattr function, TCSADRAIN option */
520 case TCSETSF
: /* Posix tcsetattr function, TCSAFLUSH option */
521 size
= sizeof(struct termios
);
524 case TCSBRK
: /* Posix tcsendbreak function */
525 case TCFLOW
: /* Posix tcflow function */
526 case TCFLSH
: /* Posix tcflush function */
527 case TIOCGPGRP
: /* Posix tcgetpgrp function */
528 case TIOCSPGRP
: /* Posix tcsetpgrp function */
532 case TIOCGWINSZ
: /* get window size (not Posix) */
533 case TIOCSWINSZ
: /* set window size (not Posix) */
534 size
= sizeof(struct winsize
);
537 #if (MACHINE == IBM_PC)
538 case KIOCSMAP
: /* load keymap (Minix extension) */
539 size
= sizeof(keymap_t
);
542 case TIOCSFON
: /* load font (Minix extension) */
543 size
= sizeof(u8_t
[8192]);
547 case TCDRAIN
: /* Posix tcdrain function -- no parameter */
552 switch (m_ptr
->TTY_REQUEST
) {
554 /* Get the termios attributes. */
556 r
= sys_safecopyto(m_ptr
->IO_ENDPT
, (vir_bytes
) m_ptr
->ADDRESS
, 0,
557 (vir_bytes
) &tp
->tty_termios
, (vir_bytes
) size
, D
);
559 r
= sys_vircopy(SELF
, D
, (vir_bytes
) &tp
->tty_termios
,
560 m_ptr
->IO_ENDPT
, D
, (vir_bytes
) m_ptr
->ADDRESS
,
568 if (tp
->tty_outleft
> 0) {
569 /* Wait for all ongoing output processing to finish. */
570 tp
->tty_iocaller
= m_ptr
->m_source
;
571 tp
->tty_ioproc
= m_ptr
->IO_ENDPT
;
572 tp
->tty_ioreq
= m_ptr
->REQUEST
;
573 tp
->tty_iovir_g
= (vir_bytes
) m_ptr
->ADDRESS
;
574 tp
->tty_io_safe
= safe
;
578 if (m_ptr
->TTY_REQUEST
== TCDRAIN
) break;
579 if (m_ptr
->TTY_REQUEST
== TCSETSF
) tty_icancel(tp
);
582 /* Set the termios attributes. */
584 r
= sys_safecopyfrom(m_ptr
->IO_ENDPT
, (vir_bytes
) m_ptr
->ADDRESS
, 0,
585 (vir_bytes
) &tp
->tty_termios
, (vir_bytes
) size
, D
);
587 r
= sys_vircopy( m_ptr
->IO_ENDPT
, D
, (vir_bytes
) m_ptr
->ADDRESS
,
588 SELF
, D
, (vir_bytes
) &tp
->tty_termios
, (vir_bytes
) size
);
596 r
= sys_safecopyfrom(m_ptr
->IO_ENDPT
, (vir_bytes
) m_ptr
->ADDRESS
, 0,
597 (vir_bytes
) ¶m
.i
, (vir_bytes
) size
, D
);
599 r
= sys_vircopy(m_ptr
->IO_ENDPT
, D
, (vir_bytes
) m_ptr
->ADDRESS
,
600 SELF
, D
, (vir_bytes
) ¶m
.i
, (vir_bytes
) size
);
604 case TCIFLUSH
: tty_icancel(tp
); break;
605 case TCOFLUSH
: (*tp
->tty_ocancel
)(tp
, 0); break;
606 case TCIOFLUSH
: tty_icancel(tp
); (*tp
->tty_ocancel
)(tp
, 0); break;
613 r
= sys_safecopyfrom(m_ptr
->IO_ENDPT
, (vir_bytes
) m_ptr
->ADDRESS
, 0,
614 (vir_bytes
) ¶m
.i
, (vir_bytes
) size
, D
);
616 r
= sys_vircopy( m_ptr
->IO_ENDPT
, D
, (vir_bytes
) m_ptr
->ADDRESS
,
617 SELF
, D
, (vir_bytes
) ¶m
.i
, (vir_bytes
) size
);
623 tp
->tty_inhibited
= (param
.i
== TCOOFF
);
627 (*tp
->tty_echo
)(tp
, tp
->tty_termios
.c_cc
[VSTOP
]);
630 (*tp
->tty_echo
)(tp
, tp
->tty_termios
.c_cc
[VSTART
]);
638 if (tp
->tty_break
!= NULL
) (*tp
->tty_break
)(tp
,0);
643 r
= sys_safecopyto(m_ptr
->IO_ENDPT
, (vir_bytes
) m_ptr
->ADDRESS
, 0,
644 (vir_bytes
) &tp
->tty_winsize
, (vir_bytes
) size
, D
);
646 r
= sys_vircopy(SELF
, D
, (vir_bytes
) &tp
->tty_winsize
,
647 m_ptr
->IO_ENDPT
, D
, (vir_bytes
) m_ptr
->ADDRESS
,
654 r
= sys_safecopyfrom(m_ptr
->IO_ENDPT
, (vir_bytes
) m_ptr
->ADDRESS
, 0,
655 (vir_bytes
) &tp
->tty_winsize
, (vir_bytes
) size
, D
);
657 r
= sys_vircopy( m_ptr
->IO_ENDPT
, D
, (vir_bytes
) m_ptr
->ADDRESS
,
658 SELF
, D
, (vir_bytes
) &tp
->tty_winsize
, (vir_bytes
) size
);
660 sigchar(tp
, SIGWINCH
);
663 #if (MACHINE == IBM_PC)
665 /* Load a new keymap (only /dev/console). */
666 if (isconsole(tp
)) r
= kbd_loadmap(m_ptr
, safe
);
670 printf("TTY: old TIOCSFON ignored.\n");
673 /* Load a font into an EGA or VGA card (hs@hck.hr) */
674 if (isconsole(tp
)) r
= con_loadfont(m_ptr
);
678 #if (MACHINE == ATARI)
680 r
= vdu_loadfont(m_ptr
);
684 /* These Posix functions are allowed to fail if _POSIX_JOB_CONTROL is
693 /* Send the reply. */
694 tty_reply(TASK_REPLY
, m_ptr
->m_source
, m_ptr
->IO_ENDPT
, r
);
697 /*===========================================================================*
699 *===========================================================================*/
700 PRIVATE
void do_open(tp
, m_ptr
)
702 message
*m_ptr
; /* pointer to message sent to task */
704 /* A tty line has been opened. Make it the callers controlling tty if
705 * O_NOCTTY is *not* set and it is not the log device. 1 is returned if
706 * the tty is made the controlling tty, otherwise OK or an error code.
710 if (m_ptr
->TTY_LINE
== LOG_MINOR
) {
711 /* The log device is a write-only diagnostics device. */
712 if (m_ptr
->COUNT
& R_BIT
) r
= EACCES
;
714 if (!(m_ptr
->COUNT
& O_NOCTTY
)) {
715 tp
->tty_pgrp
= m_ptr
->IO_ENDPT
;
720 tty_reply(TASK_REPLY
, m_ptr
->m_source
, m_ptr
->IO_ENDPT
, r
);
723 /*===========================================================================*
725 *===========================================================================*/
726 PRIVATE
void do_close(tp
, m_ptr
)
728 message
*m_ptr
; /* pointer to message sent to task */
730 /* A tty line has been closed. Clean up the line if it is the last close. */
732 if (m_ptr
->TTY_LINE
!= LOG_MINOR
&& --tp
->tty_openct
== 0) {
735 (*tp
->tty_ocancel
)(tp
, 0);
736 (*tp
->tty_close
)(tp
, 0);
737 tp
->tty_termios
= termios_defaults
;
738 tp
->tty_winsize
= winsize_defaults
;
741 tty_reply(TASK_REPLY
, m_ptr
->m_source
, m_ptr
->IO_ENDPT
, OK
);
744 /*===========================================================================*
746 *===========================================================================*/
747 PRIVATE
void do_cancel(tp
, m_ptr
)
749 message
*m_ptr
; /* pointer to message sent to task */
751 /* A signal has been sent to a process that is hanging trying to read or write.
752 * The pending read or write must be finished off immediately.
759 /* Check the parameters carefully, to avoid cancelling twice. */
760 proc_nr
= m_ptr
->IO_ENDPT
;
762 if ((mode
& R_BIT
) && tp
->tty_inleft
!= 0 && proc_nr
== tp
->tty_inproc
&&
763 (!tp
->tty_in_safe
|| tp
->tty_in_vir_g
==(vir_bytes
)m_ptr
->IO_GRANT
)) {
764 /* Process was reading when killed. Clean up input. */
766 r
= tp
->tty_incum
> 0 ? tp
->tty_incum
: EAGAIN
;
767 tp
->tty_inleft
= tp
->tty_incum
= tp
->tty_inrevived
= 0;
769 if ((mode
& W_BIT
) && tp
->tty_outleft
!= 0 && proc_nr
== tp
->tty_outproc
&&
770 (!tp
->tty_out_safe
|| tp
->tty_out_vir_g
==(vir_bytes
)m_ptr
->IO_GRANT
)) {
771 /* Process was writing when killed. Clean up output. */
773 (*tp
->tty_ocancel
)(tp
, 0);
775 r
= tp
->tty_outcum
> 0 ? tp
->tty_outcum
: EAGAIN
;
776 tp
->tty_outleft
= tp
->tty_outcum
= tp
->tty_outrevived
= 0;
778 if (tp
->tty_ioreq
!= 0 && proc_nr
== tp
->tty_ioproc
) {
779 /* Process was waiting for output to drain. */
783 tty_reply(TASK_REPLY
, m_ptr
->m_source
, proc_nr
, r
);
786 PUBLIC
int select_try(struct tty
*tp
, int ops
)
790 /* Special case. If line is hung up, no operations will block.
791 * (and it can be seen as an exceptional condition.)
793 if (tp
->tty_termios
.c_ospeed
== B0
) {
798 /* will i/o not block on read? */
799 if (tp
->tty_inleft
> 0) {
800 ready_ops
|= SEL_RD
; /* EIO - no blocking */
801 } else if (tp
->tty_incount
> 0) {
802 /* Is a regular read possible? tty_incount
803 * says there is data. But a read will only succeed
804 * in canonical mode if a newline has been seen.
806 if (!(tp
->tty_termios
.c_lflag
& ICANON
) ||
814 if (tp
->tty_outleft
> 0) ready_ops
|= SEL_WR
;
815 else if ((*tp
->tty_devwrite
)(tp
, 1)) ready_ops
|= SEL_WR
;
820 PUBLIC
int select_retry(struct tty
*tp
)
822 if (tp
->tty_select_ops
&& select_try(tp
, tp
->tty_select_ops
))
823 notify(tp
->tty_select_proc
);
827 /*===========================================================================*
829 *===========================================================================*/
830 PUBLIC
void handle_events(tp
)
831 tty_t
*tp
; /* TTY to check for events. */
833 /* Handle any events pending on a TTY. These events are usually device
836 * Two kinds of events are prominent:
837 * - a character has been received from the console or an RS232 line.
838 * - an RS232 line has completed a write request (on behalf of a user).
839 * The interrupt handler may delay the interrupt message at its discretion
840 * to avoid swamping the TTY task. Messages may be overwritten when the
841 * lines are fast or when there are races between different lines, input
842 * and output, because MINIX only provides single buffering for interrupt
843 * messages (in proc.c). This is handled by explicitly checking each line
844 * for fresh input and completed output on each interrupt.
853 /* Read input and perform input processing. */
854 (*tp
->tty_devread
)(tp
, 0);
856 /* Perform output processing and write output. */
857 (*tp
->tty_devwrite
)(tp
, 0);
859 /* Ioctl waiting for some event? */
860 if (tp
->tty_ioreq
!= 0) dev_ioctl(tp
);
861 } while (tp
->tty_events
);
863 /* Transfer characters from the input queue to a waiting process. */
866 /* Reply if enough bytes are available. */
867 if (tp
->tty_incum
>= tp
->tty_min
&& tp
->tty_inleft
> 0) {
868 if (tp
->tty_inrepcode
== TTY_REVIVE
) {
869 notify(tp
->tty_incaller
);
870 tp
->tty_inrevived
= 1;
872 tty_reply(tp
->tty_inrepcode
, tp
->tty_incaller
,
873 tp
->tty_inproc
, tp
->tty_incum
);
874 tp
->tty_inleft
= tp
->tty_incum
= 0;
877 if (tp
->tty_select_ops
)
883 select_retry_pty(tp
);
887 /*===========================================================================*
889 *===========================================================================*/
890 PRIVATE
void in_transfer(tp
)
891 register tty_t
*tp
; /* pointer to terminal to read from */
893 /* Transfer bytes from the input queue to a process reading from a terminal. */
899 /* Force read to succeed if the line is hung up, looks like EOF to reader. */
900 if (tp
->tty_termios
.c_ospeed
== B0
) tp
->tty_min
= 0;
902 /* Anything to do? */
903 if (tp
->tty_inleft
== 0 || tp
->tty_eotct
< tp
->tty_min
) return;
906 while (tp
->tty_inleft
> 0 && tp
->tty_eotct
> 0) {
907 ch
= *tp
->tty_intail
;
909 if (!(ch
& IN_EOF
)) {
910 /* One character to be delivered to the user. */
913 if (++bp
== bufend(buf
)) {
914 /* Temp buffer full, copy to user space. */
915 if(tp
->tty_in_safe
) {
916 sys_safecopyto(tp
->tty_inproc
,
917 tp
->tty_in_vir_g
, tp
->tty_in_vir_offset
,
919 (vir_bytes
) buflen(buf
), D
);
920 tp
->tty_in_vir_offset
+= buflen(buf
);
922 sys_vircopy(SELF
, D
, (vir_bytes
) buf
,
923 tp
->tty_inproc
, D
, tp
->tty_in_vir_g
,
924 (vir_bytes
) buflen(buf
));
925 tp
->tty_in_vir_g
+= buflen(buf
);
927 tp
->tty_incum
+= buflen(buf
);
932 /* Remove the character from the input queue. */
933 if (++tp
->tty_intail
== bufend(tp
->tty_inbuf
))
934 tp
->tty_intail
= tp
->tty_inbuf
;
938 /* Don't read past a line break in canonical mode. */
939 if (tp
->tty_termios
.c_lflag
& ICANON
) tp
->tty_inleft
= 0;
944 /* Leftover characters in the buffer. */
946 if(tp
->tty_in_safe
) {
947 sys_safecopyto(tp
->tty_inproc
,
948 tp
->tty_in_vir_g
, tp
->tty_in_vir_offset
,
949 (vir_bytes
) buf
, (vir_bytes
) count
, D
);
950 tp
->tty_in_vir_offset
+= count
;
952 sys_vircopy(SELF
, D
, (vir_bytes
) buf
,
953 tp
->tty_inproc
, D
, tp
->tty_in_vir_g
, (vir_bytes
) count
);
954 tp
->tty_in_vir_g
+= count
;
956 tp
->tty_incum
+= count
;
959 /* Usually reply to the reader, possibly even if incum == 0 (EOF). */
960 if (tp
->tty_inleft
== 0) {
961 if (tp
->tty_inrepcode
== TTY_REVIVE
) {
962 notify(tp
->tty_incaller
);
963 tp
->tty_inrevived
= 1;
965 tty_reply(tp
->tty_inrepcode
, tp
->tty_incaller
,
966 tp
->tty_inproc
, tp
->tty_incum
);
967 tp
->tty_inleft
= tp
->tty_incum
= 0;
972 /*===========================================================================*
974 *===========================================================================*/
975 PUBLIC
int in_process(tp
, buf
, count
)
976 register tty_t
*tp
; /* terminal on which character has arrived */
977 char *buf
; /* buffer with input characters */
978 int count
; /* number of input characters */
980 /* Characters have just been typed in. Process, save, and echo them. Return
981 * the number of characters processed.
986 static unsigned char csize_mask
[] = { 0x1F, 0x3F, 0x7F, 0xFF };
988 for (ct
= 0; ct
< count
; ct
++) {
989 /* Take one character. */
992 /* Strip to seven bits? */
993 if (tp
->tty_termios
.c_iflag
& ISTRIP
) ch
&= 0x7F;
995 /* Input extensions? */
996 if (tp
->tty_termios
.c_lflag
& IEXTEN
) {
998 /* Previous character was a character escape? */
999 if (tp
->tty_escaped
) {
1000 tp
->tty_escaped
= NOT_ESCAPED
;
1001 ch
|= IN_ESC
; /* protect character */
1004 /* LNEXT (^V) to escape the next character? */
1005 if (ch
== tp
->tty_termios
.c_cc
[VLNEXT
]) {
1006 tp
->tty_escaped
= ESCAPED
;
1009 continue; /* do not store the escape */
1012 /* REPRINT (^R) to reprint echoed characters? */
1013 if (ch
== tp
->tty_termios
.c_cc
[VREPRINT
]) {
1019 /* _POSIX_VDISABLE is a normal character value, so better escape it. */
1020 if (ch
== _POSIX_VDISABLE
) ch
|= IN_ESC
;
1022 /* Map CR to LF, ignore CR, or map LF to CR. */
1024 if (tp
->tty_termios
.c_iflag
& IGNCR
) continue;
1025 if (tp
->tty_termios
.c_iflag
& ICRNL
) ch
= '\n';
1028 if (tp
->tty_termios
.c_iflag
& INLCR
) ch
= '\r';
1031 /* Canonical mode? */
1032 if (tp
->tty_termios
.c_lflag
& ICANON
) {
1034 /* Erase processing (rub out of last character). */
1035 if (ch
== tp
->tty_termios
.c_cc
[VERASE
]) {
1036 (void) back_over(tp
);
1037 if (!(tp
->tty_termios
.c_lflag
& ECHOE
)) {
1038 (void) tty_echo(tp
, ch
);
1043 /* Kill processing (remove current line). */
1044 if (ch
== tp
->tty_termios
.c_cc
[VKILL
]) {
1045 while (back_over(tp
)) {}
1046 if (!(tp
->tty_termios
.c_lflag
& ECHOE
)) {
1047 (void) tty_echo(tp
, ch
);
1048 if (tp
->tty_termios
.c_lflag
& ECHOK
)
1054 /* EOF (^D) means end-of-file, an invisible "line break". */
1055 if (ch
== tp
->tty_termios
.c_cc
[VEOF
]) ch
|= IN_EOT
| IN_EOF
;
1057 /* The line may be returned to the user after an LF. */
1058 if (ch
== '\n') ch
|= IN_EOT
;
1060 /* Same thing with EOL, whatever it may be. */
1061 if (ch
== tp
->tty_termios
.c_cc
[VEOL
]) ch
|= IN_EOT
;
1064 /* Start/stop input control? */
1065 if (tp
->tty_termios
.c_iflag
& IXON
) {
1067 /* Output stops on STOP (^S). */
1068 if (ch
== tp
->tty_termios
.c_cc
[VSTOP
]) {
1069 tp
->tty_inhibited
= STOPPED
;
1074 /* Output restarts on START (^Q) or any character if IXANY. */
1075 if (tp
->tty_inhibited
) {
1076 if (ch
== tp
->tty_termios
.c_cc
[VSTART
]
1077 || (tp
->tty_termios
.c_iflag
& IXANY
)) {
1078 tp
->tty_inhibited
= RUNNING
;
1080 if (ch
== tp
->tty_termios
.c_cc
[VSTART
])
1086 if (tp
->tty_termios
.c_lflag
& ISIG
) {
1087 /* Check for INTR (^?) and QUIT (^\) characters. */
1088 if (ch
== tp
->tty_termios
.c_cc
[VINTR
]
1089 || ch
== tp
->tty_termios
.c_cc
[VQUIT
]) {
1091 if (ch
== tp
->tty_termios
.c_cc
[VQUIT
]) sig
= SIGQUIT
;
1093 (void) tty_echo(tp
, ch
);
1098 /* Is there space in the input buffer? */
1099 if (tp
->tty_incount
== buflen(tp
->tty_inbuf
)) {
1100 /* No space; discard in canonical mode, keep in raw mode. */
1101 if (tp
->tty_termios
.c_lflag
& ICANON
) continue;
1105 if (!(tp
->tty_termios
.c_lflag
& ICANON
)) {
1106 /* In raw mode all characters are "line breaks". */
1109 /* Start an inter-byte timer? */
1110 if (!timeset
&& tp
->tty_termios
.c_cc
[VMIN
] > 0
1111 && tp
->tty_termios
.c_cc
[VTIME
] > 0) {
1117 /* Perform the intricate function of echoing. */
1118 if (tp
->tty_termios
.c_lflag
& (ECHO
|ECHONL
)) ch
= tty_echo(tp
, ch
);
1120 /* Save the character in the input queue. */
1121 *tp
->tty_inhead
++ = ch
;
1122 if (tp
->tty_inhead
== bufend(tp
->tty_inbuf
))
1123 tp
->tty_inhead
= tp
->tty_inbuf
;
1125 if (ch
& IN_EOT
) tp
->tty_eotct
++;
1127 /* Try to finish input if the queue threatens to overflow. */
1128 if (tp
->tty_incount
== buflen(tp
->tty_inbuf
)) in_transfer(tp
);
1133 /*===========================================================================*
1135 *===========================================================================*/
1136 PRIVATE
int tty_echo(tp
, ch
)
1137 register tty_t
*tp
; /* terminal on which to echo */
1138 register int ch
; /* pointer to character to echo */
1140 /* Echo the character if echoing is on. Some control characters are echoed
1141 * with their normal effect, other control characters are echoed as "^X",
1142 * normal characters are echoed normally. EOF (^D) is echoed, but immediately
1143 * backspaced over. Return the character with the echoed length added to its
1149 if (!(tp
->tty_termios
.c_lflag
& ECHO
)) {
1150 if (ch
== ('\n' | IN_EOT
) && (tp
->tty_termios
.c_lflag
1151 & (ICANON
|ECHONL
)) == (ICANON
|ECHONL
))
1152 (*tp
->tty_echo
)(tp
, '\n');
1156 /* "Reprint" tells if the echo output has been messed up by other output. */
1157 rp
= tp
->tty_incount
== 0 ? FALSE
: tp
->tty_reprint
;
1159 if ((ch
& IN_CHAR
) < ' ') {
1160 switch (ch
& (IN_ESC
|IN_EOF
|IN_EOT
|IN_CHAR
)) {
1164 (*tp
->tty_echo
)(tp
, ' ');
1166 } while (len
< TAB_SIZE
&& (tp
->tty_position
& TAB_MASK
) != 0);
1170 (*tp
->tty_echo
)(tp
, ch
& IN_CHAR
);
1174 (*tp
->tty_echo
)(tp
, '^');
1175 (*tp
->tty_echo
)(tp
, '@' + (ch
& IN_CHAR
));
1179 if ((ch
& IN_CHAR
) == '\177') {
1180 /* A DEL prints as "^?". */
1181 (*tp
->tty_echo
)(tp
, '^');
1182 (*tp
->tty_echo
)(tp
, '?');
1185 (*tp
->tty_echo
)(tp
, ch
& IN_CHAR
);
1188 if (ch
& IN_EOF
) while (len
> 0) { (*tp
->tty_echo
)(tp
, '\b'); len
--; }
1190 tp
->tty_reprint
= rp
;
1191 return(ch
| (len
<< IN_LSHIFT
));
1194 /*===========================================================================*
1196 *===========================================================================*/
1197 PRIVATE
void rawecho(tp
, ch
)
1201 /* Echo without interpretation if ECHO is set. */
1202 int rp
= tp
->tty_reprint
;
1203 if (tp
->tty_termios
.c_lflag
& ECHO
) (*tp
->tty_echo
)(tp
, ch
);
1204 tp
->tty_reprint
= rp
;
1207 /*===========================================================================*
1209 *===========================================================================*/
1210 PRIVATE
int back_over(tp
)
1213 /* Backspace to previous character on screen and erase it. */
1217 if (tp
->tty_incount
== 0) return(0); /* queue empty */
1218 head
= tp
->tty_inhead
;
1219 if (head
== tp
->tty_inbuf
) head
= bufend(tp
->tty_inbuf
);
1220 if (*--head
& IN_EOT
) return(0); /* can't erase "line breaks" */
1221 if (tp
->tty_reprint
) reprint(tp
); /* reprint if messed up */
1222 tp
->tty_inhead
= head
;
1224 if (tp
->tty_termios
.c_lflag
& ECHOE
) {
1225 len
= (*head
& IN_LEN
) >> IN_LSHIFT
;
1233 return(1); /* one character erased */
1236 /*===========================================================================*
1238 *===========================================================================*/
1239 PRIVATE
void reprint(tp
)
1240 register tty_t
*tp
; /* pointer to tty struct */
1242 /* Restore what has been echoed to screen before if the user input has been
1243 * messed up by output, or if REPRINT (^R) is typed.
1248 tp
->tty_reprint
= FALSE
;
1250 /* Find the last line break in the input. */
1251 head
= tp
->tty_inhead
;
1252 count
= tp
->tty_incount
;
1254 if (head
== tp
->tty_inbuf
) head
= bufend(tp
->tty_inbuf
);
1255 if (head
[-1] & IN_EOT
) break;
1259 if (count
== tp
->tty_incount
) return; /* no reason to reprint */
1261 /* Show REPRINT (^R) and move to a new line. */
1262 (void) tty_echo(tp
, tp
->tty_termios
.c_cc
[VREPRINT
] | IN_ESC
);
1266 /* Reprint from the last break onwards. */
1268 if (head
== bufend(tp
->tty_inbuf
)) head
= tp
->tty_inbuf
;
1269 *head
= tty_echo(tp
, *head
);
1272 } while (count
< tp
->tty_incount
);
1275 /*===========================================================================*
1277 *===========================================================================*/
1278 PUBLIC
void out_process(tp
, bstart
, bpos
, bend
, icount
, ocount
)
1280 char *bstart
, *bpos
, *bend
; /* start/pos/end of circular buffer */
1281 int *icount
; /* # input chars / input chars used */
1282 int *ocount
; /* max output chars / output chars used */
1284 /* Perform output processing on a circular buffer. *icount is the number of
1285 * bytes to process, and the number of bytes actually processed on return.
1286 * *ocount is the space available on input and the space used on output.
1287 * (Naturally *icount < *ocount.) The column position is updated modulo
1288 * the TAB size, because we really only need it for tabs.
1294 int pos
= tp
->tty_position
;
1307 if ((tp
->tty_termios
.c_oflag
& (OPOST
|ONLCR
))
1309 /* Map LF to CR+LF if there is space. Note that the
1310 * next character in the buffer is overwritten, so
1311 * we stop at this point.
1315 if (++bpos
== bend
) bpos
= bstart
;
1321 goto out_done
; /* no space or buffer got changed */
1325 /* Best guess for the tab length. */
1326 tablen
= TAB_SIZE
- (pos
& TAB_MASK
);
1328 if ((tp
->tty_termios
.c_oflag
& (OPOST
|XTABS
))
1330 /* Tabs must be expanded. */
1331 if (oct
>= tablen
) {
1337 if (++bpos
== bend
) bpos
= bstart
;
1338 } while (--tablen
!= 0);
1342 /* Tabs are output directly. */
1346 /* Assume any other character prints as one character. */
1349 if (++bpos
== bend
) bpos
= bstart
;
1354 tp
->tty_position
= pos
& TAB_MASK
;
1356 *icount
-= ict
; /* [io]ct are the number of chars not used */
1357 *ocount
-= oct
; /* *[io]count are the number of chars that are used */
1360 /*===========================================================================*
1362 *===========================================================================*/
1363 PRIVATE
void dev_ioctl(tp
)
1366 /* The ioctl's TCSETSW, TCSETSF and TCDRAIN wait for output to finish to make
1367 * sure that an attribute change doesn't affect the processing of current
1368 * output. Once output finishes the ioctl is executed as in do_ioctl().
1372 if (tp
->tty_outleft
> 0) return; /* output not finished */
1374 if (tp
->tty_ioreq
!= TCDRAIN
) {
1375 if (tp
->tty_ioreq
== TCSETSF
) tty_icancel(tp
);
1376 if(tp
->tty_io_safe
) {
1377 result
= sys_safecopyfrom(tp
->tty_ioproc
, tp
->tty_iovir_g
, 0,
1378 (vir_bytes
) &tp
->tty_termios
,
1379 (vir_bytes
) sizeof(tp
->tty_termios
), D
);
1381 result
= sys_vircopy(tp
->tty_ioproc
, D
, tp
->tty_iovir_g
,
1382 SELF
, D
, (vir_bytes
) &tp
->tty_termios
,
1383 (vir_bytes
) sizeof(tp
->tty_termios
));
1388 notify(tp
->tty_iocaller
);
1389 tp
->tty_iorevived
= 1;
1390 tp
->tty_iostatus
= result
;
1393 /*===========================================================================*
1395 *===========================================================================*/
1396 PRIVATE
void setattr(tp
)
1399 /* Apply the new line attributes (raw/canonical, line speed, etc.) */
1403 if (!(tp
->tty_termios
.c_lflag
& ICANON
)) {
1404 /* Raw mode; put a "line break" on all characters in the input queue.
1405 * It is undefined what happens to the input queue when ICANON is
1406 * switched off, a process should use TCSAFLUSH to flush the queue.
1407 * Keeping the queue to preserve typeahead is the Right Thing, however
1408 * when a process does use TCSANOW to switch to raw mode.
1410 count
= tp
->tty_eotct
= tp
->tty_incount
;
1411 inp
= tp
->tty_intail
;
1414 if (++inp
== bufend(tp
->tty_inbuf
)) inp
= tp
->tty_inbuf
;
1419 /* Inspect MIN and TIME. */
1420 settimer(tp
, FALSE
);
1421 if (tp
->tty_termios
.c_lflag
& ICANON
) {
1422 /* No MIN & TIME in canonical mode. */
1425 /* In raw mode MIN is the number of chars wanted, and TIME how long
1426 * to wait for them. With interesting exceptions if either is zero.
1428 tp
->tty_min
= tp
->tty_termios
.c_cc
[VMIN
];
1429 if (tp
->tty_min
== 0 && tp
->tty_termios
.c_cc
[VTIME
] > 0)
1433 if (!(tp
->tty_termios
.c_iflag
& IXON
)) {
1434 /* No start/stop output control, so don't leave output inhibited. */
1435 tp
->tty_inhibited
= RUNNING
;
1439 /* Setting the output speed to zero hangs up the phone. */
1440 if (tp
->tty_termios
.c_ospeed
== B0
) sigchar(tp
, SIGHUP
);
1442 /* Set new line speed, character size, etc at the device level. */
1443 (*tp
->tty_ioctl
)(tp
, 0);
1446 /*===========================================================================*
1448 *===========================================================================*/
1451 file
, line
, code
, replyee
, proc_nr
, status
)
1454 int code
; /* TASK_REPLY or REVIVE */
1455 int replyee
; /* destination address for the reply */
1456 int proc_nr
; /* to whom should the reply go? */
1457 int status
; /* reply code */
1459 /* Send a reply to a process that wanted to read or write data. */
1462 tty_mess
.m_type
= code
;
1463 tty_mess
.REP_ENDPT
= proc_nr
;
1464 tty_mess
.REP_STATUS
= status
;
1466 /* TTY is not supposed to send a TTY_REVIVE message. The
1467 * REVIVE message is gone, TTY_REVIVE is only used as an internal
1468 * placeholder for something that is not supposed to be a message.
1470 if(code
== TTY_REVIVE
) {
1472 printf("%s:%d: ", file
, line
);
1473 panic("TTY","tty_reply sending TTY_REVIVE", NO_NUM
);
1476 if ((status
= send(replyee
, &tty_mess
)) != OK
) {
1477 printf("tty: tty_reply to %d failed: %d\n", replyee
, status
);
1481 /*===========================================================================*
1483 *===========================================================================*/
1484 PUBLIC
void sigchar(tp
, sig
)
1486 int sig
; /* SIGINT, SIGQUIT, SIGKILL or SIGHUP */
1488 /* Process a SIGINT, SIGQUIT or SIGKILL char from the keyboard or SIGHUP from
1489 * a tty close, "stty 0", or a real RS-232 hangup. MM will send the signal to
1490 * the process group (INT, QUIT), all processes (KILL), or the session leader
1495 if (tp
->tty_pgrp
!= 0) {
1496 if (OK
!= (status
= sys_kill(tp
->tty_pgrp
, sig
))) {
1497 panic("TTY","Error, call to sys_kill failed", status
);
1501 if (!(tp
->tty_termios
.c_lflag
& NOFLSH
)) {
1502 tp
->tty_incount
= tp
->tty_eotct
= 0; /* kill earlier input */
1503 tp
->tty_intail
= tp
->tty_inhead
;
1504 (*tp
->tty_ocancel
)(tp
, 0); /* kill all output */
1505 tp
->tty_inhibited
= RUNNING
;
1510 /*===========================================================================*
1512 *===========================================================================*/
1513 PRIVATE
void tty_icancel(tp
)
1516 /* Discard all pending input, tty buffer or device. */
1518 tp
->tty_incount
= tp
->tty_eotct
= 0;
1519 tp
->tty_intail
= tp
->tty_inhead
;
1520 (*tp
->tty_icancel
)(tp
, 0);
1523 /*===========================================================================*
1525 *===========================================================================*/
1526 PRIVATE
void tty_init()
1528 /* Initialize tty structure and call device initialization routines. */
1532 struct sigaction sa
;
1534 /* Initialize the terminal lines. */
1535 for (tp
= FIRST_TTY
,s
=0; tp
< END_TTY
; tp
++,s
++) {
1539 tmr_inittimer(&tp
->tty_tmr
);
1541 tp
->tty_intail
= tp
->tty_inhead
= tp
->tty_inbuf
;
1543 tp
->tty_termios
= termios_defaults
;
1544 tp
->tty_icancel
= tp
->tty_ocancel
= tp
->tty_ioctl
= tp
->tty_close
=
1546 if (tp
< tty_addr(NR_CONS
)) {
1549 /* Initialize the keyboard driver. */
1552 tp
->tty_minor
= CONS_MINOR
+ s
;
1554 if (tp
< tty_addr(NR_CONS
+NR_RS_LINES
)) {
1556 tp
->tty_minor
= RS232_MINOR
+ s
-NR_CONS
;
1559 tp
->tty_minor
= s
- (NR_CONS
+NR_RS_LINES
) + TTYPX_MINOR
;
1564 /* Install signal handlers. Ask PM to transform signal into message. */
1565 sa
.sa_handler
= SIG_MESS
;
1566 sigemptyset(&sa
.sa_mask
);
1568 if (sigaction(SIGTERM
,&sa
,NULL
)<0) panic("TTY","sigaction failed", errno
);
1569 if (sigaction(SIGKMESS
,&sa
,NULL
)<0) panic("TTY","sigaction failed", errno
);
1570 if (sigaction(SIGKSTOP
,&sa
,NULL
)<0) panic("TTY","sigaction failed", errno
);
1573 printf("end of tty_init\n");
1577 /*===========================================================================*
1579 *===========================================================================*/
1580 PRIVATE
void tty_timed_out(timer_t
*tp
)
1582 /* This timer has expired. Set the events flag, to force processing. */
1584 tty_ptr
= &tty_table
[tmr_arg(tp
)->ta_int
];
1585 tty_ptr
->tty_min
= 0; /* force read to succeed */
1586 tty_ptr
->tty_events
= 1;
1589 /*===========================================================================*
1591 *===========================================================================*/
1592 PRIVATE
void expire_timers(void)
1594 /* A synchronous alarm message was received. Check if there are any expired
1595 * timers. Possibly set the event flag and reschedule another alarm.
1597 clock_t now
; /* current time */
1600 /* Get the current time to compare the timers against. */
1601 if ((s
=getuptime(&now
)) != OK
)
1602 panic("TTY","Couldn't get uptime from clock.", s
);
1604 /* Scan the queue of timers for expired timers. This dispatch the watchdog
1605 * functions of expired timers. Possibly a new alarm call must be scheduled.
1607 tmrs_exptimers(&tty_timers
, now
, NULL
);
1608 if (tty_timers
== NULL
) tty_next_timeout
= TMR_NEVER
;
1609 else { /* set new sync alarm */
1610 tty_next_timeout
= tty_timers
->tmr_exp_time
;
1611 if ((s
=sys_setalarm(tty_next_timeout
, 1)) != OK
)
1612 panic("TTY","Couldn't set synchronous alarm.", s
);
1616 /*===========================================================================*
1618 *===========================================================================*/
1619 PRIVATE
void settimer(tty_ptr
, enable
)
1620 tty_t
*tty_ptr
; /* line to set or unset a timer on */
1621 int enable
; /* set timer if true, otherwise unset */
1623 clock_t now
; /* current time */
1627 /* Get the current time to calculate the timeout time. */
1628 if ((s
=getuptime(&now
)) != OK
)
1629 panic("TTY","Couldn't get uptime from clock.", s
);
1631 exp_time
= now
+ tty_ptr
->tty_termios
.c_cc
[VTIME
] * (HZ
/10);
1632 /* Set a new timer for enabling the TTY events flags. */
1633 tmrs_settimer(&tty_timers
, &tty_ptr
->tty_tmr
,
1634 exp_time
, tty_timed_out
, NULL
);
1636 /* Remove the timer from the active and expired lists. */
1637 tmrs_clrtimer(&tty_timers
, &tty_ptr
->tty_tmr
, NULL
);
1640 /* Now check if a new alarm must be scheduled. This happens when the front
1641 * of the timers queue was disabled or reinserted at another position, or
1642 * when a new timer was added to the front.
1644 if (tty_timers
== NULL
) tty_next_timeout
= TMR_NEVER
;
1645 else if (tty_timers
->tmr_exp_time
!= tty_next_timeout
) {
1646 tty_next_timeout
= tty_timers
->tmr_exp_time
;
1647 if ((s
=sys_setalarm(tty_next_timeout
, 1)) != OK
)
1648 panic("TTY","Couldn't set synchronous alarm.", s
);
1652 /*===========================================================================*
1654 *===========================================================================*/
1655 PUBLIC
int tty_devnop(tp
, try)
1659 /* Some functions need not be implemented at the device level. */
1662 /*===========================================================================*
1664 *===========================================================================*/
1665 PRIVATE
void do_select(tp
, m_ptr
)
1666 register tty_t
*tp
; /* pointer to tty struct */
1667 register message
*m_ptr
; /* pointer to message sent to the task */
1669 int ops
, ready_ops
= 0, watch
;
1671 ops
= m_ptr
->IO_ENDPT
& (SEL_RD
|SEL_WR
|SEL_ERR
);
1672 watch
= (m_ptr
->IO_ENDPT
& SEL_NOTIFY
) ? 1 : 0;
1674 ready_ops
= select_try(tp
, ops
);
1676 if (!ready_ops
&& ops
&& watch
) {
1677 tp
->tty_select_ops
|= ops
;
1678 tp
->tty_select_proc
= m_ptr
->m_source
;
1681 tty_reply(TASK_REPLY
, m_ptr
->m_source
, m_ptr
->IO_ENDPT
, ready_ops
);