1433d6423SLionel Sambuc /* tty.h - Terminals */ 2433d6423SLionel Sambuc 3433d6423SLionel Sambuc #include <minix/chardriver.h> 4433d6423SLionel Sambuc #include <minix/timers.h> 5433d6423SLionel Sambuc 6433d6423SLionel Sambuc /* First minor numbers for the various classes of TTY devices. */ 7da21d850SDavid van Moolenbroek #define PTMX_MINOR 0 /* minor of the Unix98 clone device */ 8433d6423SLionel Sambuc #define TTYPX_MINOR 128 9433d6423SLionel Sambuc #define PTYPX_MINOR 192 10*180e7470SDavid van Moolenbroek /* UNIX98_MINOR is defined in minix/dmap.h */ 11433d6423SLionel Sambuc 12433d6423SLionel Sambuc #define TTY_IN_BYTES 256 /* tty input queue size */ 13391516ddSDavid van Moolenbroek #define TTY_OUT_BYTES 2048 /* tty output queue size */ 14433d6423SLionel Sambuc #define TAB_SIZE 8 /* distance between tab stops */ 15433d6423SLionel Sambuc #define TAB_MASK 7 /* mask to compute a tab stop position */ 16433d6423SLionel Sambuc 17433d6423SLionel Sambuc #define ESC '\33' /* escape */ 18433d6423SLionel Sambuc 19433d6423SLionel Sambuc struct tty; 20433d6423SLionel Sambuc typedef int (*devfun_t)(struct tty *tp, int try_only); 21433d6423SLionel Sambuc typedef void (*devfunarg_t)(struct tty *tp, int c); 22da21d850SDavid van Moolenbroek typedef int (*devfunline_t)(struct tty *tp, devminor_t line); 23433d6423SLionel Sambuc 24433d6423SLionel Sambuc typedef struct tty { 25433d6423SLionel Sambuc int tty_events; /* set when TTY should inspect this line */ 26433d6423SLionel Sambuc int tty_index; /* index into TTY table */ 27433d6423SLionel Sambuc 28433d6423SLionel Sambuc /* Input queue. Typed characters are stored here until read by a program. */ 29433d6423SLionel Sambuc u16_t *tty_inhead; /* pointer to place where next char goes */ 30433d6423SLionel Sambuc u16_t *tty_intail; /* pointer to next char to be given to prog */ 31433d6423SLionel Sambuc int tty_incount; /* # chars in the input queue */ 32433d6423SLionel Sambuc int tty_eotct; /* number of "line breaks" in input queue */ 33433d6423SLionel Sambuc devfun_t tty_devread; /* routine to read from low level buffers */ 34433d6423SLionel Sambuc devfun_t tty_icancel; /* cancel any device input */ 35433d6423SLionel Sambuc int tty_min; /* minimum requested #chars in input queue */ 36433d6423SLionel Sambuc minix_timer_t tty_tmr; /* the timer for this tty */ 37433d6423SLionel Sambuc 38433d6423SLionel Sambuc /* Output section. */ 39433d6423SLionel Sambuc devfun_t tty_devwrite; /* routine to start actual device output */ 40433d6423SLionel Sambuc devfunarg_t tty_echo; /* routine to echo characters input */ 41433d6423SLionel Sambuc devfun_t tty_ocancel; /* cancel any ongoing device output */ 42433d6423SLionel Sambuc devfun_t tty_break_on; /* let the device assert a break */ 43433d6423SLionel Sambuc devfun_t tty_break_off; /* let the device de-assert a break */ 44433d6423SLionel Sambuc 45433d6423SLionel Sambuc /* Terminal parameters and status. */ 46433d6423SLionel Sambuc int tty_position; /* current position on the screen for echoing */ 47433d6423SLionel Sambuc char tty_reprint; /* 1 when echoed input messed up, else 0 */ 48433d6423SLionel Sambuc char tty_escaped; /* 1 when LNEXT (^V) just seen, else 0 */ 49433d6423SLionel Sambuc char tty_inhibited; /* 1 when STOP (^S) just seen (stops output) */ 50433d6423SLionel Sambuc endpoint_t tty_pgrp; /* endpoint of controlling process */ 51433d6423SLionel Sambuc char tty_openct; /* count of number of opens of this tty */ 52433d6423SLionel Sambuc 53433d6423SLionel Sambuc /* Information about incomplete I/O requests is stored here. */ 54433d6423SLionel Sambuc endpoint_t tty_incaller; /* process that made the call, or NONE */ 55433d6423SLionel Sambuc cdev_id_t tty_inid; /* ID of suspended read request */ 56433d6423SLionel Sambuc cp_grant_id_t tty_ingrant; /* grant where data is to go */ 57433d6423SLionel Sambuc size_t tty_inleft; /* how many chars are still needed */ 58433d6423SLionel Sambuc size_t tty_incum; /* # chars input so far */ 59433d6423SLionel Sambuc endpoint_t tty_outcaller; /* process that made the call, or NONE */ 60433d6423SLionel Sambuc cdev_id_t tty_outid; /* ID of suspended write request */ 61433d6423SLionel Sambuc cp_grant_id_t tty_outgrant; /* grant where data comes from */ 62433d6423SLionel Sambuc size_t tty_outleft; /* # chars yet to be output */ 63433d6423SLionel Sambuc size_t tty_outcum; /* # chars output so far */ 64433d6423SLionel Sambuc endpoint_t tty_iocaller; /* process that made the call, or NONE */ 65433d6423SLionel Sambuc cdev_id_t tty_ioid; /* ID of suspended ioctl request */ 66433d6423SLionel Sambuc unsigned int tty_ioreq; /* ioctl request code */ 67433d6423SLionel Sambuc cp_grant_id_t tty_iogrant; /* virtual address of ioctl buffer or grant */ 68433d6423SLionel Sambuc 69433d6423SLionel Sambuc /* select() data */ 70433d6423SLionel Sambuc unsigned int tty_select_ops; /* which operations are interesting */ 71433d6423SLionel Sambuc endpoint_t tty_select_proc; /* which process wants notification */ 72433d6423SLionel Sambuc devminor_t tty_select_minor; /* minor used to start select query */ 73433d6423SLionel Sambuc 74433d6423SLionel Sambuc /* Miscellaneous. */ 75da21d850SDavid van Moolenbroek devfunline_t tty_mayopen; /* check whether this tty may be opened */ 76433d6423SLionel Sambuc devfun_t tty_open; /* tell the device that the tty is opened */ 77433d6423SLionel Sambuc devfun_t tty_close; /* tell the device that the tty is closed */ 78433d6423SLionel Sambuc void *tty_priv; /* pointer to per device private data */ 79433d6423SLionel Sambuc struct termios tty_termios; /* terminal attributes */ 80433d6423SLionel Sambuc struct winsize tty_winsize; /* window size (#lines and #columns) */ 81433d6423SLionel Sambuc 82433d6423SLionel Sambuc u16_t tty_inbuf[TTY_IN_BYTES];/* tty input buffer */ 83433d6423SLionel Sambuc 84433d6423SLionel Sambuc } tty_t; 85433d6423SLionel Sambuc 86433d6423SLionel Sambuc /* Memory allocated in tty.c, so extern here. */ 87433d6423SLionel Sambuc extern tty_t tty_table[NR_PTYS]; 88433d6423SLionel Sambuc extern u32_t system_hz; /* system clock frequency */ 89da21d850SDavid van Moolenbroek extern int tty_gid; /* group ID of the "tty" group */ 90433d6423SLionel Sambuc 91433d6423SLionel Sambuc /* Values for the fields. */ 92433d6423SLionel Sambuc #define NOT_ESCAPED 0 /* previous character is not LNEXT (^V) */ 93433d6423SLionel Sambuc #define ESCAPED 1 /* previous character was LNEXT (^V) */ 94433d6423SLionel Sambuc #define RUNNING 0 /* no STOP (^S) has been typed to stop output */ 95433d6423SLionel Sambuc #define STOPPED 1 /* STOP (^S) has been typed to stop output */ 96433d6423SLionel Sambuc 97433d6423SLionel Sambuc /* Fields and flags on characters in the input queue. */ 98433d6423SLionel Sambuc #define IN_CHAR 0x00FF /* low 8 bits are the character itself */ 99433d6423SLionel Sambuc #define IN_LEN 0x0F00 /* length of char if it has been echoed */ 100433d6423SLionel Sambuc #define IN_LSHIFT 8 /* length = (c & IN_LEN) >> IN_LSHIFT */ 101433d6423SLionel Sambuc #define IN_EOT 0x1000 /* char is a line break (^D, LF) */ 102433d6423SLionel Sambuc #define IN_EOF 0x2000 /* char is EOF (^D), do not return to user */ 103433d6423SLionel Sambuc #define IN_ESC 0x4000 /* escaped by LNEXT (^V), no interpretation */ 104433d6423SLionel Sambuc 105433d6423SLionel Sambuc /* Number of elements and limit of a buffer. */ 106433d6423SLionel Sambuc #define buflen(buf) (sizeof(buf) / sizeof((buf)[0])) 107433d6423SLionel Sambuc #define bufend(buf) ((buf) + buflen(buf)) 108433d6423SLionel Sambuc 109433d6423SLionel Sambuc /* Function prototypes for TTY driver. */ 110433d6423SLionel Sambuc /* tty.c */ 111433d6423SLionel Sambuc void handle_events(struct tty *tp); 112433d6423SLionel Sambuc void sigchar(struct tty *tp, int sig, int mayflush); 113433d6423SLionel Sambuc void tty_task(void); 114433d6423SLionel Sambuc tty_t *line2tty(devminor_t minor); 115433d6423SLionel Sambuc int in_process(struct tty *tp, char *buf, int count); 116433d6423SLionel Sambuc void out_process(struct tty *tp, char *bstart, char *bpos, char *bend, 117433d6423SLionel Sambuc int *icount, int *ocount); 118433d6423SLionel Sambuc void tty_wakeup(clock_t now); 119433d6423SLionel Sambuc int select_try(struct tty *tp, int ops); 120433d6423SLionel Sambuc int select_retry(struct tty *tp); 121da21d850SDavid van Moolenbroek int tty_ioctl(devminor_t minor, unsigned long request, endpoint_t endpt, 122da21d850SDavid van Moolenbroek cp_grant_id_t grant, int flags, endpoint_t user_endpt, cdev_id_t id); 123433d6423SLionel Sambuc 124433d6423SLionel Sambuc /* pty.c */ 125433d6423SLionel Sambuc void do_pty(message *m_ptr, int ipc_status); 126433d6423SLionel Sambuc void pty_init(struct tty *tp); 127433d6423SLionel Sambuc void select_retry_pty(struct tty *tp); 128