tty.h revision 188487
11541Srgrimes/*- 2181905Sed * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org> 397379Sdes * All rights reserved. 497379Sdes * 5181905Sed * Portions of this software were developed under sponsorship from Snow 6181905Sed * B.V., the Netherlands. 797379Sdes * 81541Srgrimes * Redistribution and use in source and binary forms, with or without 91541Srgrimes * modification, are permitted provided that the following conditions 101541Srgrimes * are met: 111541Srgrimes * 1. Redistributions of source code must retain the above copyright 121541Srgrimes * notice, this list of conditions and the following disclaimer. 131541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141541Srgrimes * notice, this list of conditions and the following disclaimer in the 151541Srgrimes * documentation and/or other materials provided with the distribution. 161541Srgrimes * 17181905Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 181541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20181905Sed * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 211541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271541Srgrimes * SUCH DAMAGE. 281541Srgrimes * 2950477Speter * $FreeBSD: head/sys/sys/tty.h 188487 2009-02-11 16:28:49Z ed $ 301541Srgrimes */ 311541Srgrimes 322165Spaul#ifndef _SYS_TTY_H_ 334825Sbde#define _SYS_TTY_H_ 342165Spaul 35181907Sed#include <sys/param.h> 3659288Sjlemon#include <sys/queue.h> 37181907Sed#include <sys/lock.h> 38181907Sed#include <sys/mutex.h> 39181907Sed#include <sys/condvar.h> 4070834Swollman#include <sys/selinfo.h> 41181907Sed#include <sys/termios.h> 42181907Sed#include <sys/ttycom.h> 43181907Sed#include <sys/ttyqueue.h> 441541Srgrimes 45135377Sphkstruct cdev; 46181907Sedstruct file; 47181907Sedstruct pgrp; 48181907Sedstruct session; 49181907Sedstruct ucred; 50131387Smarcel 51181907Sedstruct ttydevsw; 52131373Sphk 531541Srgrimes/* 54181907Sed * Per-TTY structure, containing buffers, etc. 551541Srgrimes * 56181907Sed * List of locks 57181907Sed * (t) locked by t_mtx 58181907Sed * (l) locked by tty_list_sx 59181907Sed * (c) const until freeing 601541Srgrimes */ 611541Srgrimesstruct tty { 62181907Sed struct mtx *t_mtx; /* TTY lock. */ 63181907Sed struct mtx t_mtxobj; /* Per-TTY lock (when not borrowing). */ 64181907Sed TAILQ_ENTRY(tty) t_list; /* (l) TTY list entry. */ 65181907Sed unsigned int t_flags; /* (t) Terminal option flags. */ 66183922Sed/* Keep flags in sync with db_show_tty and pstat(8). */ 67188147Sed#define TF_NOPREFIX 0x00001 /* Don't prepend "tty" to device name. */ 68188147Sed#define TF_INITLOCK 0x00002 /* Create init/lock state devices. */ 69188147Sed#define TF_CALLOUT 0x00004 /* Create "cua" devices. */ 70188147Sed#define TF_OPENED_IN 0x00008 /* "tty" node is in use. */ 71188147Sed#define TF_OPENED_OUT 0x00010 /* "cua" node is in use. */ 72188147Sed#define TF_OPENED_CONS 0x00020 /* Device in use as console. */ 73188147Sed#define TF_OPENED (TF_OPENED_IN|TF_OPENED_OUT|TF_OPENED_CONS) 74188147Sed#define TF_GONE 0x00040 /* Device node is gone. */ 75188147Sed#define TF_OPENCLOSE 0x00080 /* Device is in open()/close(). */ 76188147Sed#define TF_ASYNC 0x00100 /* Asynchronous I/O enabled. */ 77188147Sed#define TF_LITERAL 0x00200 /* Accept the next character literally. */ 78188147Sed#define TF_HIWAT_IN 0x00400 /* We've reached the input watermark. */ 79188147Sed#define TF_HIWAT_OUT 0x00800 /* We've reached the output watermark. */ 80181907Sed#define TF_HIWAT (TF_HIWAT_IN|TF_HIWAT_OUT) 81188147Sed#define TF_STOPPED 0x01000 /* Output flow control - stopped. */ 82188147Sed#define TF_EXCLUDE 0x02000 /* Exclusive access. */ 83188147Sed#define TF_BYPASS 0x04000 /* Optimized input path. */ 84188147Sed#define TF_ZOMBIE 0x08000 /* Modem disconnect received. */ 85188147Sed#define TF_HOOK 0x10000 /* TTY has hook attached. */ 86188487Sed#define TF_BUSY_IN 0x20000 /* Process busy in read() -- not supported. */ 87188487Sed#define TF_BUSY_OUT 0x40000 /* Process busy in write(). */ 88188487Sed#define TF_BUSY (TF_BUSY_IN|TF_BUSY_OUT) 89181907Sed unsigned int t_revokecnt; /* (t) revoke() count. */ 90130261Sphk 91181907Sed /* Buffering mechanisms. */ 92181907Sed struct ttyinq t_inq; /* (t) Input queue. */ 93181907Sed size_t t_inlow; /* (t) Input low watermark. */ 94181907Sed struct ttyoutq t_outq; /* (t) Output queue. */ 95181907Sed size_t t_outlow; /* (t) Output low watermark. */ 96131373Sphk 97181907Sed /* Sleeping mechanisms. */ 98181907Sed struct cv t_inwait; /* (t) Input wait queue. */ 99181907Sed struct cv t_outwait; /* (t) Output wait queue. */ 100181907Sed struct cv t_bgwait; /* (t) Background wait queue. */ 101181907Sed struct cv t_dcdwait; /* (t) Carrier Detect wait queue. */ 1021541Srgrimes 103181907Sed /* Polling mechanisms. */ 104181907Sed struct selinfo t_inpoll; /* (t) Input poll queue. */ 105181907Sed struct selinfo t_outpoll; /* (t) Output poll queue. */ 106181907Sed struct sigio *t_sigio; /* (t) Asynchronous I/O. */ 1071541Srgrimes 108181907Sed struct termios t_termios; /* (t) I/O processing flags. */ 109181907Sed struct winsize t_winsize; /* (t) Window size. */ 110181907Sed unsigned int t_column; /* (t) Current cursor position. */ 111181907Sed unsigned int t_writepos; /* (t) Where input was interrupted. */ 112182763Sed int t_compatflags; /* (t) COMPAT_43TTY flags. */ 1131541Srgrimes 114181907Sed /* Init/lock-state devices. */ 115181907Sed struct termios t_termios_init_in; /* tty%s.init. */ 116181907Sed struct termios t_termios_lock_in; /* tty%s.lock. */ 117181907Sed struct termios t_termios_init_out; /* cua%s.init. */ 118181907Sed struct termios t_termios_lock_out; /* cua%s.lock. */ 119181907Sed 120181907Sed struct ttydevsw *t_devsw; /* (c) Driver hooks. */ 121183276Sed struct ttyhook *t_hook; /* (t) Capture/inject hook. */ 122181907Sed 123181907Sed /* Process signal delivery. */ 124181907Sed struct pgrp *t_pgrp; /* (t) Foreground process group. */ 125181907Sed struct session *t_session; /* (t) Associated session. */ 126181907Sed unsigned int t_sessioncnt; /* (t) Backpointing sessions. */ 127181907Sed 128183276Sed void *t_devswsoftc; /* (c) Soft config, for drivers. */ 129183276Sed void *t_hooksoftc; /* (t) Soft config, for hooks. */ 130181907Sed struct cdev *t_dev; /* (c) Primary character device. */ 13197366Sdes}; 13297366Sdes 13397366Sdes/* 134181907Sed * Userland version of struct tty, for sysctl kern.ttys 1357850Sbde */ 136181907Sedstruct xtty { 137181907Sed size_t xt_size; /* Structure size. */ 138181907Sed size_t xt_insize; /* Input queue size. */ 139181907Sed size_t xt_incc; /* Canonicalized characters. */ 140181907Sed size_t xt_inlc; /* Input line charaters. */ 141181907Sed size_t xt_inlow; /* Input low watermark. */ 142181907Sed size_t xt_outsize; /* Output queue size. */ 143181907Sed size_t xt_outcc; /* Output queue usage. */ 144181907Sed size_t xt_outlow; /* Output low watermark. */ 145181907Sed unsigned int xt_column; /* Current column position. */ 146181907Sed pid_t xt_pgid; /* Foreground process group. */ 147181907Sed pid_t xt_sid; /* Session. */ 148181907Sed unsigned int xt_flags; /* Terminal option flags. */ 149181907Sed dev_t xt_dev; /* Userland device. */ 1501541Srgrimes}; 1511541Srgrimes 15255205Speter#ifdef _KERNEL 153135368Sphk 154181907Sed/* Allocation and deallocation. */ 155183274Sedstruct tty *tty_alloc(struct ttydevsw *tsw, void *softc, struct mtx *mtx); 156183274Sedvoid tty_rel_pgrp(struct tty *tp, struct pgrp *pgrp); 157183274Sedvoid tty_rel_sess(struct tty *tp, struct session *sess); 158183274Sedvoid tty_rel_gone(struct tty *tp); 159135368Sphk 160181907Sed#define tty_lock(tp) mtx_lock((tp)->t_mtx) 161181907Sed#define tty_unlock(tp) mtx_unlock((tp)->t_mtx) 162181907Sed#define tty_lock_assert(tp,ma) mtx_assert((tp)->t_mtx, (ma)) 163183332Sed#define tty_getlock(tp) ((tp)->t_mtx) 164135368Sphk 165181907Sed/* Device node creation. */ 166183274Sedvoid tty_makedev(struct tty *tp, struct ucred *cred, const char *fmt, ...) 167181907Sed __printflike(3, 4); 168181907Sed#define tty_makealias(tp,fmt,...) \ 169181907Sed make_dev_alias((tp)->t_dev, fmt, ## __VA_ARGS__) 1701541Srgrimes 171181907Sed/* Signalling processes. */ 172183274Sedvoid tty_signal_sessleader(struct tty *tp, int signal); 173183274Sedvoid tty_signal_pgrp(struct tty *tp, int signal); 174181907Sed/* Waking up readers/writers. */ 175183274Sedint tty_wait(struct tty *tp, struct cv *cv); 176183274Sedint tty_timedwait(struct tty *tp, struct cv *cv, int timo); 177183274Sedvoid tty_wakeup(struct tty *tp, int flags); 1787430Sbde 179181907Sed/* System messages. */ 180183274Sedint tty_checkoutq(struct tty *tp); 181183274Sedint tty_putchar(struct tty *tp, char c); 182151385Sphk 183183274Sedint tty_ioctl(struct tty *tp, u_long cmd, void *data, struct thread *td); 184183274Sedint tty_ioctl_compat(struct tty *tp, u_long cmd, caddr_t data, 185183274Sed struct thread *td); 186183274Sedvoid tty_init_console(struct tty *tp, speed_t speed); 187183274Sedvoid tty_flush(struct tty *tp, int flags); 188183274Sedvoid tty_hiwat_in_block(struct tty *tp); 189183274Sedvoid tty_hiwat_in_unblock(struct tty *tp); 190183274Seddev_t tty_udev(struct tty *tp); 191181907Sed#define tty_opened(tp) ((tp)->t_flags & TF_OPENED) 192181907Sed#define tty_gone(tp) ((tp)->t_flags & TF_GONE) 193183276Sed#define tty_softc(tp) ((tp)->t_devswsoftc) 194181907Sed#define tty_devname(tp) devtoname((tp)->t_dev) 195151385Sphk 196181907Sed/* Status line printing. */ 197183274Sedvoid tty_info(struct tty *tp); 198151385Sphk 199184521Sed/* /dev/console selection. */ 200184521Sedvoid ttyconsdev_select(const char *name); 201184521Sed 202181907Sed/* Pseudo-terminal hooks. */ 203183274Sedint pts_alloc_external(int fd, struct thread *td, struct file *fp, 204183274Sed struct cdev *dev, const char *name); 205151385Sphk 206181907Sed/* Drivers and line disciplines also need to call these. */ 207183276Sed#include <sys/ttydisc.h> 208181907Sed#include <sys/ttydevsw.h> 209183276Sed#include <sys/ttyhook.h> 21055205Speter#endif /* _KERNEL */ 2112165Spaul 2124825Sbde#endif /* !_SYS_TTY_H_ */ 213