tty.h revision 194771
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 194771 2009-06-23 21:43:02Z 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. */ 100194771Sed struct cv t_outserwait; /* (t) Serial output wait queue. */ 101181907Sed struct cv t_bgwait; /* (t) Background wait queue. */ 102181907Sed struct cv t_dcdwait; /* (t) Carrier Detect wait queue. */ 1031541Srgrimes 104181907Sed /* Polling mechanisms. */ 105181907Sed struct selinfo t_inpoll; /* (t) Input poll queue. */ 106181907Sed struct selinfo t_outpoll; /* (t) Output poll queue. */ 107181907Sed struct sigio *t_sigio; /* (t) Asynchronous I/O. */ 1081541Srgrimes 109181907Sed struct termios t_termios; /* (t) I/O processing flags. */ 110181907Sed struct winsize t_winsize; /* (t) Window size. */ 111181907Sed unsigned int t_column; /* (t) Current cursor position. */ 112181907Sed unsigned int t_writepos; /* (t) Where input was interrupted. */ 113182763Sed int t_compatflags; /* (t) COMPAT_43TTY flags. */ 1141541Srgrimes 115181907Sed /* Init/lock-state devices. */ 116181907Sed struct termios t_termios_init_in; /* tty%s.init. */ 117181907Sed struct termios t_termios_lock_in; /* tty%s.lock. */ 118181907Sed struct termios t_termios_init_out; /* cua%s.init. */ 119181907Sed struct termios t_termios_lock_out; /* cua%s.lock. */ 120181907Sed 121181907Sed struct ttydevsw *t_devsw; /* (c) Driver hooks. */ 122183276Sed struct ttyhook *t_hook; /* (t) Capture/inject hook. */ 123181907Sed 124181907Sed /* Process signal delivery. */ 125181907Sed struct pgrp *t_pgrp; /* (t) Foreground process group. */ 126181907Sed struct session *t_session; /* (t) Associated session. */ 127181907Sed unsigned int t_sessioncnt; /* (t) Backpointing sessions. */ 128181907Sed 129183276Sed void *t_devswsoftc; /* (c) Soft config, for drivers. */ 130183276Sed void *t_hooksoftc; /* (t) Soft config, for hooks. */ 131181907Sed struct cdev *t_dev; /* (c) Primary character device. */ 13297366Sdes}; 13397366Sdes 13497366Sdes/* 135181907Sed * Userland version of struct tty, for sysctl kern.ttys 1367850Sbde */ 137181907Sedstruct xtty { 138181907Sed size_t xt_size; /* Structure size. */ 139181907Sed size_t xt_insize; /* Input queue size. */ 140181907Sed size_t xt_incc; /* Canonicalized characters. */ 141181907Sed size_t xt_inlc; /* Input line charaters. */ 142181907Sed size_t xt_inlow; /* Input low watermark. */ 143181907Sed size_t xt_outsize; /* Output queue size. */ 144181907Sed size_t xt_outcc; /* Output queue usage. */ 145181907Sed size_t xt_outlow; /* Output low watermark. */ 146181907Sed unsigned int xt_column; /* Current column position. */ 147181907Sed pid_t xt_pgid; /* Foreground process group. */ 148181907Sed pid_t xt_sid; /* Session. */ 149181907Sed unsigned int xt_flags; /* Terminal option flags. */ 150181907Sed dev_t xt_dev; /* Userland device. */ 1511541Srgrimes}; 1521541Srgrimes 15355205Speter#ifdef _KERNEL 154135368Sphk 155181907Sed/* Allocation and deallocation. */ 156193018Sedstruct tty *tty_alloc(struct ttydevsw *tsw, void *softc); 157193018Sedstruct tty *tty_alloc_mutex(struct ttydevsw *tsw, void *softc, struct mtx *mtx); 158183274Sedvoid tty_rel_pgrp(struct tty *tp, struct pgrp *pgrp); 159183274Sedvoid tty_rel_sess(struct tty *tp, struct session *sess); 160183274Sedvoid tty_rel_gone(struct tty *tp); 161135368Sphk 162181907Sed#define tty_lock(tp) mtx_lock((tp)->t_mtx) 163181907Sed#define tty_unlock(tp) mtx_unlock((tp)->t_mtx) 164181907Sed#define tty_lock_assert(tp,ma) mtx_assert((tp)->t_mtx, (ma)) 165183332Sed#define tty_getlock(tp) ((tp)->t_mtx) 166135368Sphk 167181907Sed/* Device node creation. */ 168183274Sedvoid tty_makedev(struct tty *tp, struct ucred *cred, const char *fmt, ...) 169181907Sed __printflike(3, 4); 170181907Sed#define tty_makealias(tp,fmt,...) \ 171181907Sed make_dev_alias((tp)->t_dev, fmt, ## __VA_ARGS__) 1721541Srgrimes 173181907Sed/* Signalling processes. */ 174183274Sedvoid tty_signal_sessleader(struct tty *tp, int signal); 175183274Sedvoid tty_signal_pgrp(struct tty *tp, int signal); 176181907Sed/* Waking up readers/writers. */ 177183274Sedint tty_wait(struct tty *tp, struct cv *cv); 178183274Sedint tty_timedwait(struct tty *tp, struct cv *cv, int timo); 179183274Sedvoid tty_wakeup(struct tty *tp, int flags); 1807430Sbde 181181907Sed/* System messages. */ 182183274Sedint tty_checkoutq(struct tty *tp); 183183274Sedint tty_putchar(struct tty *tp, char c); 184151385Sphk 185183274Sedint tty_ioctl(struct tty *tp, u_long cmd, void *data, struct thread *td); 186183274Sedint tty_ioctl_compat(struct tty *tp, u_long cmd, caddr_t data, 187183274Sed struct thread *td); 188183274Sedvoid tty_init_console(struct tty *tp, speed_t speed); 189183274Sedvoid tty_flush(struct tty *tp, int flags); 190183274Sedvoid tty_hiwat_in_block(struct tty *tp); 191183274Sedvoid tty_hiwat_in_unblock(struct tty *tp); 192183274Seddev_t tty_udev(struct tty *tp); 193181907Sed#define tty_opened(tp) ((tp)->t_flags & TF_OPENED) 194181907Sed#define tty_gone(tp) ((tp)->t_flags & TF_GONE) 195183276Sed#define tty_softc(tp) ((tp)->t_devswsoftc) 196181907Sed#define tty_devname(tp) devtoname((tp)->t_dev) 197151385Sphk 198181907Sed/* Status line printing. */ 199183274Sedvoid tty_info(struct tty *tp); 200151385Sphk 201184521Sed/* /dev/console selection. */ 202184521Sedvoid ttyconsdev_select(const char *name); 203184521Sed 204181907Sed/* Pseudo-terminal hooks. */ 205183274Sedint pts_alloc_external(int fd, struct thread *td, struct file *fp, 206183274Sed struct cdev *dev, const char *name); 207151385Sphk 208181907Sed/* Drivers and line disciplines also need to call these. */ 209183276Sed#include <sys/ttydisc.h> 210181907Sed#include <sys/ttydevsw.h> 211183276Sed#include <sys/ttyhook.h> 21255205Speter#endif /* _KERNEL */ 2132165Spaul 2144825Sbde#endif /* !_SYS_TTY_H_ */ 215