8000 minix/minix/drivers/tty/tty/tty.c at v3.3.0 · gezidan/minix · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
1608 lines (1399 loc) · 45.5 KB

tty.c

File metadata and controls

1608 lines (1399 loc) · 45.5 KB
return(tp);
}
/*===========================================================================*
* sef_local_startup *
*===========================================================================*/
static void sef_local_startup()
{
/* Register init callbacks. */
sef_setcb_init_fresh(sef_cb_init_fresh);
sef_setcb_init_restart(sef_cb_init_fresh);
/* No live update support for now. */
/* Register signal callbacks. */
sef_setcb_signal_handler(sef_cb_signal_handler);
/* Let SEF perform startup. */
sef_startup();
}
/*===========================================================================*
* sef_cb_init_fresh *
*===========================================================================*/
static int sef_cb_init_fresh(int UNUSED(type), sef_init_info_t *UNUSED(info))
{
/* Initialize the tty driver. */
int r;
char val[CONS_ARG];
/* Get kernel environment (protected_mode, pc_at and ega are needed). */
if (OK != (r=sys_getmachine(&machine))) {
panic("Couldn't obtain kernel environment: %d", r);
}
if (env_get_param("console", val, sizeof(val)) == OK) {
set_console_line(val);
}
if ((r = env_get_param("kernelclr", val, sizeof(val))) == OK) {
set_kernel_color(val);
}
/* Initialize the TTY driver. */
tty_init();
/* Final one-time keyboard initialization. */
kb_init_once();
/* Register for diagnostics notifications. */
sys_diagctl_register();
return(OK);
}
static void
set_console_line(char term[CONS_ARG])
{
/* Parse 'term' and redirect console output there. */
int i;
/* Console */
if (!strncmp(term, "console", CONS_ARG - 1)) {
consoleline = CONS_MINOR+0;
}
/* The other console terminals */
for (i = 1; i < NR_CONS; i++) {
char cons[6];
strlcpy(cons, "ttyc0", sizeof(cons));
cons[4] += i;
if (!strncmp(term, cons,
CONS_ARG < sizeof(cons) ? CONS_ARG-1 : sizeof(cons) - 1))
consoleline = CONS_MINOR + i;
}
/* Serial lines */
assert(NR_RS_LINES <= 9);/* below assumes this is the case */
for (i = 0; i < NR_RS_LINES; i++) {
char sercons[6];
strlcpy(sercons, "tty00", sizeof(sercons));
sercons[4] += i;
if (!strncmp(term, sercons,
CONS_ARG < sizeof(sercons) ? CONS_ARG-1:sizeof(sercons)-1))
consoleline = RS232_MINOR + i;
}
}
static void
set_kernel_color(char color[CONS_ARG])
{
int def_color;
#define SGR_COLOR_START 30
#define SGR_COLOR_END 37
def_color = atoi(color);
if ((SGR_COLOR_START + def_color) >= SGR_COLOR_START &&
(SGR_COLOR_START + def_color) <= SGR_COLOR_END) {
kernel_msg_color = def_color + SGR_COLOR_START;
}
}
static void
do_new_kmess(void)
{
/* Kernel wants to print a new message */
struct kmessages *kmess_ptr; /* kmessages structure */
char kernel_buf_copy[_KMESS_BUF_SIZE];
static int prev_next = 0;
int next, bytes, copy, restore = 0;
tty_t *tp, rtp;
assert(_minix_kerninfo);
kmess_ptr = _minix_kerninfo->kmessages;
/* The kernel buffer is circular; print only the new part. Determine
* how many new bytes there are with the help of current and
* previous 'next' index. This works fine if less than _KMESS_BUF_SIZE
* bytes is new data; else we miss % _KMESS_BUF_SIZE here. Obtain
* 'next' only once, since we are operating on shared memory here.
* Check for size being positive; the buffer might as well be emptied!
*/
next = kmess_ptr->km_next;
bytes = ((next + _KMESS_BUF_SIZE) - prev_next) % _KMESS_BUF_SIZE;
if (bytes > 0) {
/* Copy from current position toward end of buffer */
copy = MIN(_KMESS_BUF_SIZE - prev_next, bytes);
memcpy(kernel_buf_copy, &kmess_ptr->km_buf[prev_next], copy);
/* Copy remainder from start of buffer */
if (copy < bytes) {
memcpy(&kernel_buf_copy[copy], &kmess_ptr->km_buf[0],
bytes - copy);
}
tp = line2tty(consoleline);
if (tp == NULL)
panic("Don't know where to send kernel messages");
if (tp->tty_outleft > 0) {
/* Terminal is already printing */
rtp = *tp; /* Make backup */
tp->tty_outleft = 0; /* So do_write is happy */
restore = 1;
}
if (kernel_msg_color != 0)
set_color(tp, kernel_msg_color);
do_write(tp->tty_minor, 0, KERNEL,
(cp_grant_id_t) kernel_buf_copy, bytes,
CDEV_NONBLOCK, 0);
if (kernel_msg_color != 0)
reset_color(tp);
if (restore) {
*tp = rtp;
}
}
/* Store 'next' pointer so that we can determine what part of the
* kernel messages buffer to print next time a notification arrives.
*/
prev_next = next;
}
/*===========================================================================*
* sef_cb_signal_handler *
*===========================================================================*/
static void sef_cb_signal_handler(int signo)
{
/* Check for known signals, ignore anything else. */
switch(signo) {
/* There is a pending message from the kernel. */
case SIGKMESS:
do_new_kmess();
break;
/* Switch to primary console on termination. */
case SIGTERM:
cons_stop();
break;
}
}
/*===========================================================================*
* do_read *
*===========================================================================*/
static ssize_t do_read(devminor_t minor, u64_t UNUSED(position),
endpoint_t endpt, cp_grant_id_t grant, size_t size, int flags,
cdev_id_t id)
{
/* A process wants to read from a terminal. */
tty_t *tp;
int r;
if ((tp = line2tty(minor)) == NULL)
return ENXIO;
/* Check if there is already a process hanging in a read, check if the
* parameters are correct, do I/O.
*/
if (tp->tty_incaller != NONE || tp->tty_inleft > 0)
return EIO;
if (size <= 0)
return EINVAL;
/* Copy information from the message to the tty struct. */
tp->tty_incaller = endpt;
tp->tty_inid = id;
tp->tty_ingrant = grant;
assert(tp->tty_incum == 0);
tp->tty_inleft = size;
if (!(tp->tty_termios.c_lflag & ICANON) && tp->tty_termios.c_cc[VTIME] > 0) {
if (tp->tty_termios.c_cc[VMIN] == 0) {
/* MIN & TIME specify a read timer that finishes the
* read in TIME/10 seconds if no bytes are available.
*/
settimer(tp, TRUE);
tp->tty_min = 1;
} else {
/* MIN & TIME specify an inter-byte timer that may
* have to be cancelled if there are no bytes yet.
*/
if (tp->tty_eotct == 0) {
settimer(tp, FALSE);
tp->tty_min = tp->tty_termios.c_cc[VMIN];
}
}
}
/* Anything waiting in the input buffer? Clear it out... */
in_transfer(tp);
/* ...then go back for more. */
handle_events(tp);
if (tp->tty_inleft == 0)
return EDONTREPLY; /* already done */
/* There were no bytes in the input queue available. */
if (flags & CDEV_NONBLOCK) {
tty_icancel(tp);
r = tp->tty_incum > 0 ? tp->tty_incum : EAGAIN;
tp->tty_inleft = tp->tty_incum = 0;
tp->tty_incaller = NONE;
return r;
}
if (tp->tty_select_ops)
select_retry(tp);
return EDONTREPLY; /* suspend the caller */
}
/*===========================================================================*
* do_write *
*===========================================================================*/
static ssize_t do_write(devminor_t minor, u64_t UNUSED(position),
endpoint_t endpt, cp_grant_id_t grant, size_t size, int flags,
cdev_id_t id)
{
/* A process wants to write on a terminal. */
tty_t *tp;
int r;
if ((tp = line2tty(minor)) == NULL)
return ENXIO;
/* Check if there is already a process hanging in a write, check if the
* parameters are correct, do I/O.
*/
if (tp->tty_outcaller != NONE || tp->tty_outleft > 0)
return EIO;
if (size <= 0)
return EINVAL;
/* Copy message parameters to the tty structure. */
tp->tty_outcaller = endpt;
tp->tty_outid = id;
tp->tty_outgrant = grant;
assert(tp->tty_outcum == 0);
tp->tty_outleft = size;
/* Try to write. */
handle_events(tp);
if (tp->tty_outleft == 0)
return EDONTREPLY; /* already done */
/* None or not all the bytes could be written. */
if (flags & CDEV_NONBLOCK) {
r = tp->tty_outcum > 0 ? tp->tty_outcum : EAGAIN;
tp->tty_outleft = tp->tty_outcum = 0;
tp->tty_outcaller = NONE;
return r;
}
if (tp->tty_select_ops)
select_retry(tp);
return EDONTREPLY; /* suspend the caller */
}
/*===========================================================================*
* do_ioctl *
*===========================================================================*/
static int do_ioctl(devminor_t minor, unsigned long request, endpoint_t endpt,
cp_grant_id_t grant, int flags, endpoint_t user_endpt, cdev_id_t id)
{
/* Perform an IOCTL on this terminal. POSIX termios calls are handled
* by the IOCTL system call.
*/
kio_bell_t bell;
clock_t ticks;
tty_t *tp;
int i, r;
if ((tp = line2tty(minor)) == NULL)
return ENXIO;
r = OK;
switch (request) {
case TIOCGETA:
/* Get the termios attributes. */
r = sys_safecopyto(endpt, grant, 0, (vir_bytes) &tp->tty_termios,
sizeof(struct termios));
break;
case TIOCSETAW:
case TIOCSETAF:
case TIOCDRAIN:
if (tp->tty_outleft > 0) {
if (flags & CDEV_NONBLOCK)
return EAGAIN;
/* Wait for all ongoing output processing to finish. */
tp->tty_iocaller = endpt;
tp->tty_ioid = id;
tp->tty_ioreq = request;
tp->tty_iogrant = grant;
return EDONTREPLY; /* suspend the caller */
}
if (request == TIOCDRAIN) break;
if (request == TIOCSETAF) tty_icancel(tp);
/*FALL THROUGH*/
case TIOCSETA:
/* Set the termios attributes. */
r = sys_safecopyfrom(endpt, grant, 0, (vir_bytes) &tp->tty_termios,
sizeof(struct termios));
if (r != OK) break;
setattr(tp);
break;
case TIOCFLUSH:
r = sys_safecopyfrom(endpt, grant, 0, (vir_bytes) &i, sizeof(i));
if (r != OK) break;
if(i & FREAD) { tty_icancel(tp); }
if(i & FWRITE) { (*tp->tty_ocancel)(tp, 0); }
break;
case TIOCSTART:
tp->tty_inhibited = 0;
tp->tty_events = 1;
break;
case TIOCSTOP:
tp->tty_inhibited = 1;
tp->tty_events = 1;
break;
case TIOCSBRK: /* tcsendbreak - turn break on */
if (tp->tty_break_on != NULL) (*tp->tty_break_on)(tp,0);
break;
case TIOCCBRK: /* tcsendbreak - turn break off */
if (tp->tty_break_off != NULL) (*tp->tty_break_off)(tp,0);
break;
case TIOCGWINSZ:
r = sys_safecopyto(endpt, grant, 0, (vir_bytes) &tp->tty_winsize,
sizeof(struct winsize));
break;
case TIOCSWINSZ:
r = sys_safecopyfrom(endpt, grant, 0, (vir_bytes) &tp->tty_winsize,
sizeof(struct winsize));
sigchar(tp, SIGWINCH, 0);
break;
case KIOCBELL:
/* Sound bell (only /dev/console). */
if (!isconsole(tp))
break;
r = sys_safecopyfrom(endpt, grant, 0, (vir_bytes) &bell, sizeof(bell));
if (r != OK)
break;
ticks = bell.kb_duration.tv_usec * system_hz / 1000000;
ticks += bell.kb_duration.tv_sec * system_hz;
if (!ticks)
ticks++;
beep_x(bell.kb_pitch, ticks);
break;
case TIOCGETD: /* get line discipline */
i = TTYDISC;
r = sys_safecopyto(endpt, grant, 0, (vir_bytes) &i, sizeof(i));
break;
case TIOCSETD: /* set line discipline */
printf("TTY: TIOCSETD: can't set any other line discipline.\n");
r = ENOTTY;
break;
case TIOCGLINED: /* get line discipline as string */
r = sys_safecopyto(endpt, grant, 0, (vir_byt 10BC8 es) lined, sizeof(lined));
break;
case TIOCGQSIZE: /* get input/output queue sizes */
i = TTY_IN_BYTES; /* best we can do.. */
r = sys_safecopyto(endpt, grant, 0, (vir_bytes) &i, sizeof(i));
break;
case KIOCSMAP:
/* Load a new keymap (only /dev/console). */
if (isconsole(tp)) r = kbd_loadmap(endpt, grant);
break;
case TIOCSFON:
/* Load a font into an EGA or VGA card (hs@hck.hr) */
if (isconsole(tp)) r = con_loadfont(endpt, grant);
break;
case TIOCSCTTY:
/* Process sets this tty as its controlling tty */
tp->tty_pgrp = user_endpt;
break;
/* These Posix functions are allowed to fail if _POSIX_JOB_CONTROL is
* not defined.
*/
case TIOCGPGRP:
case TIOCSPGRP:
default:
r = ENOTTY;
}
return r;
}
/*===========================================================================*
* do_open *
*===========================================================================*/
static int do_open(devminor_t minor, int access, endpoint_t user_endpt)
{
/* A tty line has been opened. Make it the callers controlling tty if
* CDEV_NOCTTY is *not* set and it is not the log device. CDEV_CTTY is returned
* if the tty is made the controlling tty, otherwise OK or an error code.
*/
tty_t *tp;
int r = OK;
if ((tp = line2tty(minor)) == NULL)
return ENXIO;
if (minor == LOG_MINOR && isconsole(tp)) {
/* The log device is a write-only diagnostics device. */
if (access & CDEV_R_BIT) return EACCES;
} else {
if (!(access & CDEV_NOCTTY)) {
tp->tty_pgrp = user_endpt;
r = CDEV_CTTY;
}
tp->tty_openct++;
if (tp->tty_openct == 1) {
/* Tell the device that the tty is opened */
(*tp->tty_open)(tp, 0);
}
}
return r;
}
/*===========================================================================*
* do_close *
*===========================================================================*/
static int do_close(devminor_t minor)
{
/* A tty line has been closed. Clean up the line if it is the last close. */
tty_t *tp;
if ((tp = line2tty(minor)) == NULL)
return ENXIO;
if ((minor != LOG_MINOR || !isconsole(tp)) && --tp->tty_openct == 0) {
tp->tty_pgrp = 0;
tty_icancel(tp);
(*tp->tty_ocancel)(tp, 0);
(*tp->tty_close)(tp, 0);
tp->tty_termios = termios_defaults;
tp->tty_winsize = winsize_defaults;
setattr(tp);
}
return OK;
}
/*===========================================================================*
* do_cancel *
*===========================================================================*/
static int do_cancel(devminor_t minor, endpoint_t endpt, cdev_id_t id)
{
/* A signal has been sent to a process that is hanging trying to read or write.
* The pending read or write must be finished off immediately.
*/
tty_t *tp;
int r;
if ((tp = line2tty(minor)) == NULL)
return ENXIO;
/* Check the parameters carefully, to avoid cancelling twice. */
r = EDONTREPLY;
if (tp->tty_inleft != 0 && endpt == tp->tty_incaller && id == tp->tty_inid) {
/* Process was reading when killed. Clean up input. */
tty_icancel(tp);
r = tp->tty_incum > 0 ? tp->tty_incum : EAGAIN;
tp->tty_inleft = tp->tty_incum = 0;
tp->tty_incaller = NONE;
} else if (tp->tty_outleft != 0 && endpt == tp->tty_outcaller &&
id == tp->tty_outid) {
/* Process was writing when killed. Clean up output. */
r = tp->tty_outcum > 0 ? tp->tty_outcum : EAGAIN;
tp->tty_outleft = tp->tty_outcum = 0;
tp->tty_outcaller = NONE;
} else if (tp->tty_ioreq != 0 && endpt == tp->tty_iocaller &&
id == tp->tty_ioid) {
/* Process was waiting for output to drain. */
r = EINTR;
tp->tty_ioreq = 0;
tp->tty_iocaller = NONE;
}
if (r != EDONTREPLY)
tp->tty_events = 1;
/* Only reply if we found a matching request. */
return r;
}
int select_try(struct tty *tp, int ops)
{
int ready_ops = 0;
/* Special case. If line is hung up, no operations will block.
* (and it can be seen as an exceptional condition.)
*/
if (tp->tty_termios.c_ospeed == B0) {
ready_ops |= ops;
}
if (ops & CDEV_OP_RD) {
/* will i/o not block on read? */
if (tp->tty_inleft > 0) {
ready_ops |= CDEV_OP_RD; /* EIO - no blocking */
} else if (tp->tty_incount > 0) {
/* Is a regular read possible? tty_incount
* says there is data. But a read will only succeed
* in canonical mode if a newline has been seen.
*/
if (!(tp->tty_termios.c_lflag & ICANON) ||
tp->tty_eotct > 0) {
ready_ops |= CDEV_OP_RD;
}
}
}
if (ops & CDEV_OP_WR) {
if (tp->tty_outleft > 0) ready_ops |= CDEV_OP_WR;
else if ((*tp->tty_devwrite)(tp, 1)) ready_ops |= CDEV_OP_WR;
}
return ready_ops;
}
int select_retry(struct tty *tp)
{
int ops;
if (tp->tty_select_ops && (ops = select_try(tp, tp->tty_select_ops))) {
chardriver_reply_select(tp->tty_select_proc,
tp->tty_select_minor, ops);
tp->tty_select_ops &= ~ops;
}
return OK;
}
/*===========================================================================*
* do_select *
*===========================================================================*/
static int do_select(devminor_t minor, unsigned int ops, endpoint_t endpt)
{
tty_t *tp;
int ready_ops, watch;
if ((tp = line2tty(minor)) == NULL)
return ENXIO;
watch = (ops & CDEV_NOTIFY);
ops &= (CDEV_OP_RD | CDEV_OP_WR | CDEV_OP_ERR);
ready_ops = select_try(tp, ops);
ops &= ~ready_ops;
if (ops && watch) {
/* Translated minor numbers are a problem with late select replies. We
* have to save the minor number used to do the select, since otherwise
* VFS won't be able to make sense of those late replies. We do not
* support selecting on two different minors for the same object.
*/
if (tp->tty_select_ops != 0 && tp->tty_select_minor != minor) {
printf("TTY: select on one object with two minors (%d, %d)\n",
tp->tty_select_minor, minor);
return EBADF;
}
tp->tty_select_ops |= ops;
tp->tty_select_proc = endpt;
tp->tty_select_minor = minor;
}
return ready_ops;
}
/*===========================================================================*
* handle_events *
*===========================================================================*/
void handle_events(tp)
tty_t *tp; /* TTY to check for events. */
{
/* Handle any events pending on a TTY. These events are usually device
* interrupts.
*
* Two kinds of events are prominent:
* - a character has been received from the console or an RS232 line.
* - an RS232 line has completed a write request (on behalf of a user).
* The interrupt handler may delay the interrupt message at its discretion
* to avoid swamping the TTY task. Messages may be overwritten when the
* lines are fast or when there are races between different lines, input
* and output, because MINIX only provides single buffering for interrupt
* messages. This is handled by explicitly checking each line for fresh input
* and completed output on each interrupt.
*/
do {
tp->tty_events = 0;
/* Read input and perform input processing. */
(*tp->tty_devread)(tp, 0);
/* Perform output processing and write output. */
(*tp->tty_devwrite)(tp, 0);
/* Ioctl waiting for some event? */
if (tp->tty_ioreq != 0) dev_ioctl(tp);
} while (tp->tty_events);
/* Transfer characters from the input queue to a waiting process. */
in_transfer(tp);
/* Reply if enough bytes are available. */
if (tp->tty_incum >= tp->tty_min && tp->tty_inleft > 0) {
chardriver_reply_task(tp->tty_incaller, tp->tty_inid, tp->tty_incum);
tp->tty_inleft = tp->tty_incum = 0;
tp->tty_incaller = NONE;
}
if (tp->tty_select_ops)
{
select_retry(tp);
}
}
/*===========================================================================*
* in_transfer *
*===========================================================================*/
static void in_transfer(tp)
register tty_t *tp; /* pointer to terminal to read from */
{
/* Transfer bytes from the input queue to a process reading from a terminal. */
int ch;
int count;
char buf[64], *bp;
/* Force read to succeed if the line is hung up, looks like EOF to reader. */
if (tp->tty_termios.c_ospeed == B0) tp->tty_min = 0;
/* Anything to do? */
if (tp->tty_inleft == 0 || tp->tty_eotct < tp->tty_min) return;
bp = buf;
while (tp->tty_inleft > 0 && tp->tty_eotct > 0) {
ch = *tp->tty_intail;
if (!(ch & IN_EOF)) {
/* One character to be delivered to the user. */
*bp = ch & IN_CHAR;
tp->tty_inleft--;
if (++bp == bufend(buf)) {
/* Temp buffer full, copy to user space. */
sys_safecopyto(tp->tty_incaller,
tp->tty_ingrant, tp->tty_incum,
(vir_bytes) buf, (vir_bytes) buflen(buf));
tp->tty_incum += buflen(buf);
bp = buf;
}
}
/* Remove the character from the input queue. */
if (++tp->tty_intail == bufend(tp->tty_inbuf))
tp->tty_intail = tp->tty_inbuf;
tp->tty_incount--;
if (ch & IN_EOT) {
tp->tty_eotct--;
/* Don't read past a line break in canonical mode. */
if (tp->tty_termios.c_lflag & ICANON) tp->tty_inleft = 0;
}
}
if (bp > buf) {
/* Leftover characters in the buffer. */
count = bp - buf;
0