Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-1.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * n_tty.c --- implements the N_TTY line discipline.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * This code used to be in tty_io.c, but things are getting hairy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  * enough that it made sense to split things off.  (The N_TTY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  * processing has changed so much that it's hardly recognizable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  * anyway...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  * Note that the open routine for N_TTY is guaranteed never to return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  * an error.  This is because Linux will fall back to setting a line
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  * to N_TTY if it can not switch to any other line discipline.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14)  * Written by Theodore Ts'o, Copyright 1994.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16)  * This file also contains code originally written by Linus Torvalds,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17)  * Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19)  * Reduced memory usage for older ARM systems  - Russell King.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21)  * 2000/01/20   Fixed SMP locking on put_tty_queue using bits of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22)  *		the patch by Andrew J. Kroll <ag784@freenet.buffalo.edu>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23)  *		who actually finally proved there really was a race.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25)  * 2002/03/18   Implemented n_tty_wakeup to send SIGIO POLL_OUTs to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26)  *		waiting writing processes-Sapan Bhatia <sapan@corewars.org>.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27)  *		Also fixed a bug in BLOCKING mode where n_tty_write returns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28)  *		EAGAIN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include <linux/major.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include <linux/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #include <linux/fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #include <linux/tty.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) #include <linux/timer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) #include <linux/audit.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) #include <linux/ratelimit.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54)  * Until this number of characters is queued in the xmit buffer, select will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55)  * return "we have room for writes".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) #define WAKEUP_CHARS 256
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60)  * This defines the low- and high-watermarks for throttling and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61)  * unthrottling the TTY driver.  These watermarks are used for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62)  * controlling the space in the read buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) #define TTY_THRESHOLD_THROTTLE		128 /* now based on remaining room */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) #define TTY_THRESHOLD_UNTHROTTLE	128
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68)  * Special byte codes used in the echo buffer to represent operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69)  * or special handling of characters.  Bytes in the echo buffer that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70)  * are not part of such special blocks are treated as normal character
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71)  * codes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) #define ECHO_OP_START 0xff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) #define ECHO_OP_MOVE_BACK_COL 0x80
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) #define ECHO_OP_SET_CANON_COL 0x81
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) #define ECHO_OP_ERASE_TAB 0x82
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) #define ECHO_COMMIT_WATERMARK	256
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) #define ECHO_BLOCK		256
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) #define ECHO_DISCARD_WATERMARK	N_TTY_BUF_SIZE - (ECHO_BLOCK + 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) #undef N_TTY_TRACE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) #ifdef N_TTY_TRACE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) # define n_tty_trace(f, args...)	trace_printk(f, ##args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) # define n_tty_trace(f, args...)	no_printk(f, ##args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) struct n_tty_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 	/* producer-published */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	size_t read_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 	size_t commit_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	size_t canon_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 	size_t echo_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	size_t echo_commit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	size_t echo_mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	DECLARE_BITMAP(char_map, 256);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 	/* private to n_tty_receive_overrun (single-threaded) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 	unsigned long overrun_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	int num_overrun;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	/* non-atomic */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	bool no_room;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	/* must hold exclusive termios_rwsem to reset these */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	unsigned char push:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	/* shared by producer and consumer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	char read_buf[N_TTY_BUF_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	DECLARE_BITMAP(read_flags, N_TTY_BUF_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	unsigned char echo_buf[N_TTY_BUF_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	/* consumer-published */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 	size_t read_tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 	size_t line_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	/* protected by output lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	unsigned int column;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	unsigned int canon_column;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	size_t echo_tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	struct mutex atomic_read_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	struct mutex output_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) #define MASK(x) ((x) & (N_TTY_BUF_SIZE - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) static inline size_t read_cnt(struct n_tty_data *ldata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	return ldata->read_head - ldata->read_tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) static inline unsigned char read_buf(struct n_tty_data *ldata, size_t i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	return ldata->read_buf[i & (N_TTY_BUF_SIZE - 1)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) static inline unsigned char *read_buf_addr(struct n_tty_data *ldata, size_t i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	return &ldata->read_buf[i & (N_TTY_BUF_SIZE - 1)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) static inline unsigned char echo_buf(struct n_tty_data *ldata, size_t i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	smp_rmb(); /* Matches smp_wmb() in add_echo_byte(). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	return ldata->echo_buf[i & (N_TTY_BUF_SIZE - 1)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) static inline unsigned char *echo_buf_addr(struct n_tty_data *ldata, size_t i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	return &ldata->echo_buf[i & (N_TTY_BUF_SIZE - 1)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) /* If we are not echoing the data, perhaps this is a secret so erase it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) static void zero_buffer(struct tty_struct *tty, u8 *buffer, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	bool icanon = !!L_ICANON(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	bool no_echo = !L_ECHO(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	if (icanon && no_echo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 		memset(buffer, 0x00, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) static void tty_copy(struct tty_struct *tty, void *to, size_t tail, size_t n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	struct n_tty_data *ldata = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 	size_t size = N_TTY_BUF_SIZE - tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	void *from = read_buf_addr(ldata, tail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	if (n > size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 		tty_audit_add_data(tty, from, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 		memcpy(to, from, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 		zero_buffer(tty, from, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 		to += size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 		n -= size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 		from = ldata->read_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	tty_audit_add_data(tty, from, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	memcpy(to, from, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	zero_buffer(tty, from, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188)  *	n_tty_kick_worker - start input worker (if required)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189)  *	@tty: terminal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191)  *	Re-schedules the flip buffer work if it may have stopped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193)  *	Caller holds exclusive termios_rwsem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194)  *	   or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195)  *	n_tty_read()/consumer path:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196)  *		holds non-exclusive termios_rwsem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) static void n_tty_kick_worker(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 	struct n_tty_data *ldata = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	/* Did the input worker stop? Restart it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 	if (unlikely(ldata->no_room)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 		ldata->no_room = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 		WARN_RATELIMIT(tty->port->itty == NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 				"scheduling with invalid itty\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 		/* see if ldisc has been killed - if so, this means that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 		 * even though the ldisc has been halted and ->buf.work
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 		 * cancelled, ->buf.work is about to be rescheduled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 		WARN_RATELIMIT(test_bit(TTY_LDISC_HALTED, &tty->flags),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 			       "scheduling buffer work for halted ldisc\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 		tty_buffer_restart_work(tty->port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) static ssize_t chars_in_buffer(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	struct n_tty_data *ldata = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	ssize_t n = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	if (!ldata->icanon)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 		n = ldata->commit_head - ldata->read_tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 		n = ldata->canon_head - ldata->read_tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232)  *	n_tty_write_wakeup	-	asynchronous I/O notifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233)  *	@tty: tty device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235)  *	Required for the ptys, serial driver etc. since processes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236)  *	that attach themselves to the master and rely on ASYNC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237)  *	IO must be woken up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) static void n_tty_write_wakeup(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) static void n_tty_check_throttle(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	struct n_tty_data *ldata = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 	 * Check the remaining room for the input canonicalization
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	 * mode.  We don't want to throttle the driver if we're in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	 * canonical mode and don't have a newline yet!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	if (ldata->icanon && ldata->canon_head == ldata->read_tail)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 		int throttled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 		tty_set_flow_change(tty, TTY_THROTTLE_SAFE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 		if (N_TTY_BUF_SIZE - read_cnt(ldata) >= TTY_THRESHOLD_THROTTLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 		throttled = tty_throttle_safe(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 		if (!throttled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 	__tty_set_flow_change(tty, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) static void n_tty_check_unthrottle(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	if (tty->driver->type == TTY_DRIVER_TYPE_PTY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 		if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 		n_tty_kick_worker(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 		tty_wakeup(tty->link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	/* If there is enough space in the read buffer now, let the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 	 * low-level driver know. We use chars_in_buffer() to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	 * check the buffer, as it now knows about canonical mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	 * Otherwise, if the driver is throttled and the line is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	 * we won't get any more characters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 		int unthrottled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 		tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 		if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 		n_tty_kick_worker(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 		unthrottled = tty_unthrottle_safe(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 		if (!unthrottled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	__tty_set_flow_change(tty, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302)  *	put_tty_queue		-	add character to tty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303)  *	@c: character
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304)  *	@ldata: n_tty data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306)  *	Add a character to the tty read_buf queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308)  *	n_tty_receive_buf()/producer path:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309)  *		caller holds non-exclusive termios_rwsem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) static inline void put_tty_queue(unsigned char c, struct n_tty_data *ldata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	*read_buf_addr(ldata, ldata->read_head) = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	ldata->read_head++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319)  *	reset_buffer_flags	-	reset buffer state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320)  *	@ldata: line disc data to reset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322)  *	Reset the read buffer counters and clear the flags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323)  *	Called from n_tty_open() and n_tty_flush_buffer().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325)  *	Locking: caller holds exclusive termios_rwsem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326)  *		 (or locking is not required)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) static void reset_buffer_flags(struct n_tty_data *ldata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	ldata->read_head = ldata->canon_head = ldata->read_tail = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	ldata->commit_head = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	ldata->line_start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	ldata->erasing = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	ldata->push = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) static void n_tty_packet_mode_flush(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	if (tty->link->packet) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 		spin_lock_irqsave(&tty->ctrl_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 		tty->ctrl_status |= TIOCPKT_FLUSHREAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 		spin_unlock_irqrestore(&tty->ctrl_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 		wake_up_interruptible(&tty->link->read_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353)  *	n_tty_flush_buffer	-	clean input queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354)  *	@tty:	terminal device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356)  *	Flush the input buffer. Called when the tty layer wants the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357)  *	buffer flushed (eg at hangup) or when the N_TTY line discipline
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358)  *	internally has to clean the pending queue (for example some signals).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360)  *	Holds termios_rwsem to exclude producer/consumer while
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361)  *	buffer indices are reset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363)  *	Locking: ctrl_lock, exclusive termios_rwsem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) static void n_tty_flush_buffer(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	down_write(&tty->termios_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	reset_buffer_flags(tty->disc_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	n_tty_kick_worker(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	if (tty->link)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 		n_tty_packet_mode_flush(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	up_write(&tty->termios_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378)  *	is_utf8_continuation	-	utf8 multibyte check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379)  *	@c: byte to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381)  *	Returns true if the utf8 character 'c' is a multibyte continuation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382)  *	character. We use this to correctly compute the on screen size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383)  *	of the character when printing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) static inline int is_utf8_continuation(unsigned char c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	return (c & 0xc0) == 0x80;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392)  *	is_continuation		-	multibyte check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393)  *	@c: byte to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395)  *	Returns true if the utf8 character 'c' is a multibyte continuation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396)  *	character and the terminal is in unicode mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) static inline int is_continuation(unsigned char c, struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 	return I_IUTF8(tty) && is_utf8_continuation(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405)  *	do_output_char			-	output one character
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406)  *	@c: character (or partial unicode symbol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407)  *	@tty: terminal device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408)  *	@space: space available in tty driver write buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410)  *	This is a helper function that handles one output character
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411)  *	(including special characters like TAB, CR, LF, etc.),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412)  *	doing OPOST processing and putting the results in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413)  *	tty driver's write buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415)  *	Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416)  *	and NLDLY.  They simply aren't relevant in the world today.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417)  *	If you ever need them, add them here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419)  *	Returns the number of bytes of buffer space used or -1 if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420)  *	no space left.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422)  *	Locking: should be called under the output_lock to protect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423)  *		 the column state and space left in the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	struct n_tty_data *ldata = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	int	spaces;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	if (!space)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	switch (c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	case '\n':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 		if (O_ONLRET(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 			ldata->column = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 		if (O_ONLCR(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 			if (space < 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 				return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 			ldata->canon_column = ldata->column = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 			tty->ops->write(tty, "\r\n", 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 			return 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 		ldata->canon_column = ldata->column;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	case '\r':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 		if (O_ONOCR(tty) && ldata->column == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 		if (O_OCRNL(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 			c = '\n';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 			if (O_ONLRET(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 				ldata->canon_column = ldata->column = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 		ldata->canon_column = ldata->column = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	case '\t':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 		spaces = 8 - (ldata->column & 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 		if (O_TABDLY(tty) == XTABS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 			if (space < spaces)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 				return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 			ldata->column += spaces;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 			tty->ops->write(tty, "        ", spaces);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 			return spaces;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 		ldata->column += spaces;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	case '\b':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 		if (ldata->column > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 			ldata->column--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 		if (!iscntrl(c)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 			if (O_OLCUC(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 				c = toupper(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 			if (!is_continuation(c, tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 				ldata->column++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	tty_put_char(tty, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488)  *	process_output			-	output post processor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489)  *	@c: character (or partial unicode symbol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490)  *	@tty: terminal device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492)  *	Output one character with OPOST processing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493)  *	Returns -1 when the output device is full and the character
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494)  *	must be retried.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496)  *	Locking: output_lock to protect column state and space left
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497)  *		 (also, this is called from n_tty_write under the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498)  *		  tty layer write lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) static int process_output(unsigned char c, struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	struct n_tty_data *ldata = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	int	space, retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	mutex_lock(&ldata->output_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	space = tty_write_room(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	retval = do_output_char(c, tty, space);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	mutex_unlock(&ldata->output_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	if (retval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519)  *	process_output_block		-	block post processor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520)  *	@tty: terminal device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521)  *	@buf: character buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522)  *	@nr: number of bytes to output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524)  *	Output a block of characters with OPOST processing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525)  *	Returns the number of characters output.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527)  *	This path is used to speed up block console writes, among other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528)  *	things when processing blocks of output data. It handles only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529)  *	the simple cases normally found and helps to generate blocks of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530)  *	symbols for the console driver and thus improve performance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532)  *	Locking: output_lock to protect column state and space left
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533)  *		 (also, this is called from n_tty_write under the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534)  *		  tty layer write lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) static ssize_t process_output_block(struct tty_struct *tty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 				    const unsigned char *buf, unsigned int nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	struct n_tty_data *ldata = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	int	space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	int	i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	const unsigned char *cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	mutex_lock(&ldata->output_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	space = tty_write_room(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	if (space <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 		mutex_unlock(&ldata->output_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 		return space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	if (nr > space)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 		nr = space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	for (i = 0, cp = buf; i < nr; i++, cp++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 		unsigned char c = *cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 		switch (c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 		case '\n':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 			if (O_ONLRET(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 				ldata->column = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 			if (O_ONLCR(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 				goto break_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 			ldata->canon_column = ldata->column;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 		case '\r':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 			if (O_ONOCR(tty) && ldata->column == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 				goto break_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 			if (O_OCRNL(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 				goto break_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 			ldata->canon_column = ldata->column = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 		case '\t':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 			goto break_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 		case '\b':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 			if (ldata->column > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 				ldata->column--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 			if (!iscntrl(c)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 				if (O_OLCUC(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 					goto break_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 				if (!is_continuation(c, tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 					ldata->column++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) break_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	i = tty->ops->write(tty, buf, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 	mutex_unlock(&ldata->output_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597)  *	process_echoes	-	write pending echo characters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598)  *	@tty: terminal device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600)  *	Write previously buffered echo (and other ldisc-generated)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601)  *	characters to the tty.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603)  *	Characters generated by the ldisc (including echoes) need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604)  *	be buffered because the driver's write buffer can fill during
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605)  *	heavy program output.  Echoing straight to the driver will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606)  *	often fail under these conditions, causing lost characters and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607)  *	resulting mismatches of ldisc state information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609)  *	Since the ldisc state must represent the characters actually sent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610)  *	to the driver at the time of the write, operations like certain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611)  *	changes in column state are also saved in the buffer and executed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612)  *	here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614)  *	A circular fifo buffer is used so that the most recent characters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615)  *	are prioritized.  Also, when control characters are echoed with a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616)  *	prefixed "^", the pair is treated atomically and thus not separated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618)  *	Locking: callers must hold output_lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) static size_t __process_echoes(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	struct n_tty_data *ldata = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	int	space, old_space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	size_t tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	unsigned char c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	old_space = space = tty_write_room(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	tail = ldata->echo_tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	while (MASK(ldata->echo_commit) != MASK(tail)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 		c = echo_buf(ldata, tail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 		if (c == ECHO_OP_START) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 			unsigned char op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 			int no_space_left = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 			 * Since add_echo_byte() is called without holding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 			 * output_lock, we might see only portion of multi-byte
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 			 * operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 			if (MASK(ldata->echo_commit) == MASK(tail + 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 				goto not_yet_stored;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 			 * If the buffer byte is the start of a multi-byte
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 			 * operation, get the next byte, which is either the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 			 * op code or a control character value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 			op = echo_buf(ldata, tail + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 			switch (op) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 			case ECHO_OP_ERASE_TAB: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 				unsigned int num_chars, num_bs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 				if (MASK(ldata->echo_commit) == MASK(tail + 2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 					goto not_yet_stored;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 				num_chars = echo_buf(ldata, tail + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 				 * Determine how many columns to go back
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 				 * in order to erase the tab.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 				 * This depends on the number of columns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 				 * used by other characters within the tab
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 				 * area.  If this (modulo 8) count is from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 				 * the start of input rather than from a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 				 * previous tab, we offset by canon column.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 				 * Otherwise, tab spacing is normal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 				if (!(num_chars & 0x80))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 					num_chars += ldata->canon_column;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 				num_bs = 8 - (num_chars & 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 				if (num_bs > space) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 					no_space_left = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 				space -= num_bs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 				while (num_bs--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 					tty_put_char(tty, '\b');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 					if (ldata->column > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 						ldata->column--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 				tail += 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 			case ECHO_OP_SET_CANON_COL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 				ldata->canon_column = ldata->column;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 				tail += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 			case ECHO_OP_MOVE_BACK_COL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 				if (ldata->column > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 					ldata->column--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 				tail += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 			case ECHO_OP_START:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 				/* This is an escaped echo op start code */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 				if (!space) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 					no_space_left = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 				tty_put_char(tty, ECHO_OP_START);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 				ldata->column++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 				space--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 				tail += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 			default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 				 * If the op is not a special byte code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 				 * it is a ctrl char tagged to be echoed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 				 * as "^X" (where X is the letter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 				 * representing the control char).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 				 * Note that we must ensure there is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 				 * enough space for the whole ctrl pair.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 				 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 				if (space < 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 					no_space_left = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 				tty_put_char(tty, '^');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 				tty_put_char(tty, op ^ 0100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 				ldata->column += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 				space -= 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 				tail += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 			if (no_space_left)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 			if (O_OPOST(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 				int retval = do_output_char(c, tty, space);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 				if (retval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 				space -= retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 				if (!space)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 				tty_put_char(tty, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 				space -= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 			tail += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	/* If the echo buffer is nearly full (so that the possibility exists
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 	 * of echo overrun before the next commit), then discard enough
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	 * data at the tail to prevent a subsequent overrun */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 	while (ldata->echo_commit > tail &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 	       ldata->echo_commit - tail >= ECHO_DISCARD_WATERMARK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 		if (echo_buf(ldata, tail) == ECHO_OP_START) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 			if (echo_buf(ldata, tail + 1) == ECHO_OP_ERASE_TAB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 				tail += 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 				tail += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 			tail++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762)  not_yet_stored:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 	ldata->echo_tail = tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	return old_space - space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) static void commit_echoes(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	struct n_tty_data *ldata = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 	size_t nr, old, echoed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	size_t head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	mutex_lock(&ldata->output_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	head = ldata->echo_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	ldata->echo_mark = head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	old = ldata->echo_commit - ldata->echo_tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	/* Process committed echoes if the accumulated # of bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	 * is over the threshold (and try again each time another
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	 * block is accumulated) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	nr = head - ldata->echo_tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	if (nr < ECHO_COMMIT_WATERMARK ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 	    (nr % ECHO_BLOCK > old % ECHO_BLOCK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 		mutex_unlock(&ldata->output_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	ldata->echo_commit = head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	echoed = __process_echoes(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	mutex_unlock(&ldata->output_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	if (echoed && tty->ops->flush_chars)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 		tty->ops->flush_chars(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) static void process_echoes(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	struct n_tty_data *ldata = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	size_t echoed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 	if (ldata->echo_mark == ldata->echo_tail)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	mutex_lock(&ldata->output_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 	ldata->echo_commit = ldata->echo_mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 	echoed = __process_echoes(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 	mutex_unlock(&ldata->output_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	if (echoed && tty->ops->flush_chars)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 		tty->ops->flush_chars(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) /* NB: echo_mark and echo_head should be equivalent here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) static void flush_echoes(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	struct n_tty_data *ldata = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	if ((!L_ECHO(tty) && !L_ECHONL(tty)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	    ldata->echo_commit == ldata->echo_head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 	mutex_lock(&ldata->output_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	ldata->echo_commit = ldata->echo_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	__process_echoes(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	mutex_unlock(&ldata->output_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829)  *	add_echo_byte	-	add a byte to the echo buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830)  *	@c: unicode byte to echo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831)  *	@ldata: n_tty data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833)  *	Add a character or operation byte to the echo buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) static inline void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 	*echo_buf_addr(ldata, ldata->echo_head) = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	smp_wmb(); /* Matches smp_rmb() in echo_buf(). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	ldata->echo_head++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844)  *	echo_move_back_col	-	add operation to move back a column
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845)  *	@ldata: n_tty data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847)  *	Add an operation to the echo buffer to move back one column.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) static void echo_move_back_col(struct n_tty_data *ldata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	add_echo_byte(ECHO_OP_START, ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857)  *	echo_set_canon_col	-	add operation to set the canon column
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858)  *	@ldata: n_tty data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860)  *	Add an operation to the echo buffer to set the canon column
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861)  *	to the current column.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) static void echo_set_canon_col(struct n_tty_data *ldata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	add_echo_byte(ECHO_OP_START, ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871)  *	echo_erase_tab	-	add operation to erase a tab
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872)  *	@num_chars: number of character columns already used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873)  *	@after_tab: true if num_chars starts after a previous tab
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874)  *	@ldata: n_tty data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876)  *	Add an operation to the echo buffer to erase a tab.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878)  *	Called by the eraser function, which knows how many character
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879)  *	columns have been used since either a previous tab or the start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880)  *	of input.  This information will be used later, along with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881)  *	canon column (if applicable), to go back the correct number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882)  *	of columns.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) static void echo_erase_tab(unsigned int num_chars, int after_tab,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 			   struct n_tty_data *ldata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	add_echo_byte(ECHO_OP_START, ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	/* We only need to know this modulo 8 (tab spacing) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	num_chars &= 7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	/* Set the high bit as a flag if num_chars is after a previous tab */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	if (after_tab)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 		num_chars |= 0x80;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 	add_echo_byte(num_chars, ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902)  *	echo_char_raw	-	echo a character raw
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903)  *	@c: unicode byte to echo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904)  *	@ldata: line disc data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906)  *	Echo user input back onto the screen. This must be called only when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907)  *	L_ECHO(tty) is true. Called from the driver receive_buf path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909)  *	This variant does not treat control characters specially.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	if (c == ECHO_OP_START) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 		add_echo_byte(ECHO_OP_START, ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 		add_echo_byte(ECHO_OP_START, ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 		add_echo_byte(c, ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923)  *	echo_char	-	echo a character
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924)  *	@c: unicode byte to echo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925)  *	@tty: terminal device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927)  *	Echo user input back onto the screen. This must be called only when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928)  *	L_ECHO(tty) is true. Called from the driver receive_buf path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930)  *	This variant tags control characters to be echoed as "^X"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931)  *	(where X is the letter representing the control char).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) static void echo_char(unsigned char c, struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	struct n_tty_data *ldata = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	if (c == ECHO_OP_START) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 		add_echo_byte(ECHO_OP_START, ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 		add_echo_byte(ECHO_OP_START, ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 		if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 			add_echo_byte(ECHO_OP_START, ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 		add_echo_byte(c, ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949)  *	finish_erasing		-	complete erase
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950)  *	@ldata: n_tty data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) static inline void finish_erasing(struct n_tty_data *ldata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 	if (ldata->erasing) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 		echo_char_raw('/', ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 		ldata->erasing = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962)  *	eraser		-	handle erase function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963)  *	@c: character input
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964)  *	@tty: terminal device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966)  *	Perform erase and necessary output when an erase character is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967)  *	present in the stream from the driver layer. Handles the complexities
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968)  *	of UTF-8 multibyte symbols.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970)  *	n_tty_receive_buf()/producer path:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971)  *		caller holds non-exclusive termios_rwsem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) static void eraser(unsigned char c, struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 	struct n_tty_data *ldata = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	enum { ERASE, WERASE, KILL } kill_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	size_t head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	size_t cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	int seen_alnums;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	if (ldata->read_head == ldata->canon_head) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 		/* process_output('\a', tty); */ /* what do you think? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	if (c == ERASE_CHAR(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 		kill_type = ERASE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	else if (c == WERASE_CHAR(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 		kill_type = WERASE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 		if (!L_ECHO(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 			ldata->read_head = ldata->canon_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 		if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 			ldata->read_head = ldata->canon_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 			finish_erasing(ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 			echo_char(KILL_CHAR(tty), tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 			/* Add a newline if ECHOK is on and ECHOKE is off. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 			if (L_ECHOK(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 				echo_char_raw('\n', ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 		kill_type = KILL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	seen_alnums = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 	while (MASK(ldata->read_head) != MASK(ldata->canon_head)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 		head = ldata->read_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 		/* erase a single possibly multibyte character */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 		do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 			head--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 			c = read_buf(ldata, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 		} while (is_continuation(c, tty) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 			 MASK(head) != MASK(ldata->canon_head));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 		/* do not partially erase */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 		if (is_continuation(c, tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 		if (kill_type == WERASE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 			/* Equivalent to BSD's ALTWERASE. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 			if (isalnum(c) || c == '_')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 				seen_alnums++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 			else if (seen_alnums)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 		cnt = ldata->read_head - head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 		ldata->read_head = head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 		if (L_ECHO(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 			if (L_ECHOPRT(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 				if (!ldata->erasing) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 					echo_char_raw('\\', ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 					ldata->erasing = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 				/* if cnt > 1, output a multi-byte character */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 				echo_char(c, tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 				while (--cnt > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 					head++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 					echo_char_raw(read_buf(ldata, head), ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 					echo_move_back_col(ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 			} else if (kill_type == ERASE && !L_ECHOE(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 				echo_char(ERASE_CHAR(tty), tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 			} else if (c == '\t') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 				unsigned int num_chars = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 				int after_tab = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 				size_t tail = ldata->read_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 				 * Count the columns used for characters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 				 * since the start of input or after a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 				 * previous tab.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 				 * This info is used to go back the correct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 				 * number of columns.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 				while (MASK(tail) != MASK(ldata->canon_head)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 					tail--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 					c = read_buf(ldata, tail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 					if (c == '\t') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 						after_tab = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 						break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 					} else if (iscntrl(c)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 						if (L_ECHOCTL(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 							num_chars += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 					} else if (!is_continuation(c, tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 						num_chars++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 					}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 				echo_erase_tab(num_chars, after_tab, ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 				if (iscntrl(c) && L_ECHOCTL(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 					echo_char_raw('\b', ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 					echo_char_raw(' ', ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 					echo_char_raw('\b', ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 				if (!iscntrl(c) || L_ECHOCTL(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 					echo_char_raw('\b', ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 					echo_char_raw(' ', ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 					echo_char_raw('\b', ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 		if (kill_type == ERASE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 		finish_erasing(ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093)  *	isig		-	handle the ISIG optio
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094)  *	@sig: signal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095)  *	@tty: terminal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097)  *	Called when a signal is being sent due to terminal input.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098)  *	Called from the driver receive_buf path so serialized.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100)  *	Performs input and output flush if !NOFLSH. In this context, the echo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101)  *	buffer is 'output'. The signal is processed first to alert any current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102)  *	readers or writers to discontinue and exit their i/o loops.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104)  *	Locking: ctrl_lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) static void __isig(int sig, struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 	struct pid *tty_pgrp = tty_get_pgrp(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	if (tty_pgrp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 		kill_pgrp(tty_pgrp, sig, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 		put_pid(tty_pgrp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) static void isig(int sig, struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 	struct n_tty_data *ldata = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 	if (L_NOFLSH(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 		/* signal only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 		__isig(sig, tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	} else { /* signal and flush */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 		up_read(&tty->termios_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 		down_write(&tty->termios_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 		__isig(sig, tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 		/* clear echo buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 		mutex_lock(&ldata->output_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 		ldata->echo_head = ldata->echo_tail = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 		ldata->echo_mark = ldata->echo_commit = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 		mutex_unlock(&ldata->output_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 		/* clear output buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 		tty_driver_flush_buffer(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 		/* clear input buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 		reset_buffer_flags(tty->disc_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 		/* notify pty master of flush */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 		if (tty->link)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 			n_tty_packet_mode_flush(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 		up_write(&tty->termios_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 		down_read(&tty->termios_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152)  *	n_tty_receive_break	-	handle break
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153)  *	@tty: terminal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155)  *	An RS232 break event has been hit in the incoming bitstream. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156)  *	can cause a variety of events depending upon the termios settings.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158)  *	n_tty_receive_buf()/producer path:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159)  *		caller holds non-exclusive termios_rwsem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161)  *	Note: may get exclusive termios_rwsem if flushing input buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) static void n_tty_receive_break(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	struct n_tty_data *ldata = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 	if (I_IGNBRK(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 	if (I_BRKINT(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 		isig(SIGINT, tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	if (I_PARMRK(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 		put_tty_queue('\377', ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 		put_tty_queue('\0', ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 	put_tty_queue('\0', ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182)  *	n_tty_receive_overrun	-	handle overrun reporting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183)  *	@tty: terminal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185)  *	Data arrived faster than we could process it. While the tty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186)  *	driver has flagged this the bits that were missed are gone
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187)  *	forever.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189)  *	Called from the receive_buf path so single threaded. Does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190)  *	need locking as num_overrun and overrun_time are function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191)  *	private.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) static void n_tty_receive_overrun(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 	struct n_tty_data *ldata = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 	ldata->num_overrun++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 	if (time_after(jiffies, ldata->overrun_time + HZ) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 			time_after(ldata->overrun_time, jiffies)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 		tty_warn(tty, "%d input overrun(s)\n", ldata->num_overrun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 		ldata->overrun_time = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 		ldata->num_overrun = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208)  *	n_tty_receive_parity_error	-	error notifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209)  *	@tty: terminal device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210)  *	@c: character
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212)  *	Process a parity error and queue the right data to indicate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213)  *	the error case if necessary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215)  *	n_tty_receive_buf()/producer path:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216)  *		caller holds non-exclusive termios_rwsem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) static void n_tty_receive_parity_error(struct tty_struct *tty, unsigned char c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	struct n_tty_data *ldata = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 	if (I_INPCK(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 		if (I_IGNPAR(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 		if (I_PARMRK(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 			put_tty_queue('\377', ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 			put_tty_queue('\0', ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 			put_tty_queue(c, ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 			put_tty_queue('\0', ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 		put_tty_queue(c, ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) n_tty_receive_signal_char(struct tty_struct *tty, int signal, unsigned char c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 	isig(signal, tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 	if (I_IXON(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 		start_tty(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 	if (L_ECHO(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 		echo_char(c, tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 		commit_echoes(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 		process_echoes(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250)  *	n_tty_receive_char	-	perform processing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251)  *	@tty: terminal device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252)  *	@c: character
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254)  *	Process an individual character of input received from the driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255)  *	This is serialized with respect to itself by the rules for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256)  *	driver above.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258)  *	n_tty_receive_buf()/producer path:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259)  *		caller holds non-exclusive termios_rwsem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260)  *		publishes canon_head if canonical mode is active
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262)  *	Returns 1 if LNEXT was received, else returns 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) n_tty_receive_char_special(struct tty_struct *tty, unsigned char c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	struct n_tty_data *ldata = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 	if (I_IXON(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 		if (c == START_CHAR(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 			start_tty(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 			process_echoes(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 		if (c == STOP_CHAR(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 			stop_tty(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 	if (L_ISIG(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 		if (c == INTR_CHAR(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 			n_tty_receive_signal_char(tty, SIGINT, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 		} else if (c == QUIT_CHAR(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 			n_tty_receive_signal_char(tty, SIGQUIT, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 		} else if (c == SUSP_CHAR(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 			n_tty_receive_signal_char(tty, SIGTSTP, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 	if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 		start_tty(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 		process_echoes(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 	if (c == '\r') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 		if (I_IGNCR(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 		if (I_ICRNL(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 			c = '\n';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 	} else if (c == '\n' && I_INLCR(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 		c = '\r';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 	if (ldata->icanon) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 		if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 		    (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 			eraser(c, tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 			commit_echoes(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 		if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 			ldata->lnext = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 			if (L_ECHO(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 				finish_erasing(ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 				if (L_ECHOCTL(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 					echo_char_raw('^', ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 					echo_char_raw('\b', ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 					commit_echoes(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 		if (c == REPRINT_CHAR(tty) && L_ECHO(tty) && L_IEXTEN(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 			size_t tail = ldata->canon_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 			finish_erasing(ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 			echo_char(c, tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 			echo_char_raw('\n', ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 			while (MASK(tail) != MASK(ldata->read_head)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 				echo_char(read_buf(ldata, tail), tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 				tail++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 			commit_echoes(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 		if (c == '\n') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 			if (L_ECHO(tty) || L_ECHONL(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 				echo_char_raw('\n', ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 				commit_echoes(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 			goto handle_newline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 		if (c == EOF_CHAR(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 			c = __DISABLED_CHAR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 			goto handle_newline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 		if ((c == EOL_CHAR(tty)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 		    (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 			 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 			if (L_ECHO(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 				/* Record the column of first canon char. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 				if (ldata->canon_head == ldata->read_head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 					echo_set_canon_col(ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 				echo_char(c, tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 				commit_echoes(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 			 * XXX does PARMRK doubling happen for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 			 * EOL_CHAR and EOL2_CHAR?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 			if (c == (unsigned char) '\377' && I_PARMRK(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 				put_tty_queue(c, ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) handle_newline:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 			set_bit(ldata->read_head & (N_TTY_BUF_SIZE - 1), ldata->read_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 			put_tty_queue(c, ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 			smp_store_release(&ldata->canon_head, ldata->read_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 			kill_fasync(&tty->fasync, SIGIO, POLL_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 			wake_up_interruptible_poll(&tty->read_wait, EPOLLIN | EPOLLRDNORM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 	if (L_ECHO(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 		finish_erasing(ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 		if (c == '\n')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 			echo_char_raw('\n', ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 		else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 			/* Record the column of first canon char. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 			if (ldata->canon_head == ldata->read_head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 				echo_set_canon_col(ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 			echo_char(c, tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 		commit_echoes(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 	/* PARMRK doubling check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 	if (c == (unsigned char) '\377' && I_PARMRK(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 		put_tty_queue(c, ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 	put_tty_queue(c, ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) static inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) n_tty_receive_char_inline(struct tty_struct *tty, unsigned char c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 	struct n_tty_data *ldata = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 	if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 		start_tty(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 		process_echoes(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 	if (L_ECHO(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 		finish_erasing(ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 		/* Record the column of first canon char. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 		if (ldata->canon_head == ldata->read_head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 			echo_set_canon_col(ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 		echo_char(c, tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 		commit_echoes(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 	/* PARMRK doubling check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 	if (c == (unsigned char) '\377' && I_PARMRK(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 		put_tty_queue(c, ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 	put_tty_queue(c, ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) static void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 	n_tty_receive_char_inline(tty, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) static inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) n_tty_receive_char_fast(struct tty_struct *tty, unsigned char c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 	struct n_tty_data *ldata = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 	if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 		start_tty(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 		process_echoes(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 	if (L_ECHO(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 		finish_erasing(ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 		/* Record the column of first canon char. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 		if (ldata->canon_head == ldata->read_head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 			echo_set_canon_col(ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 		echo_char(c, tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 		commit_echoes(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 	put_tty_queue(c, ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) static void n_tty_receive_char_closing(struct tty_struct *tty, unsigned char c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 	if (I_ISTRIP(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 		c &= 0x7f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 	if (I_IUCLC(tty) && L_IEXTEN(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 		c = tolower(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 	if (I_IXON(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 		if (c == STOP_CHAR(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 			stop_tty(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 		else if (c == START_CHAR(tty) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 			 (tty->stopped && !tty->flow_stopped && I_IXANY(tty) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 			  c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 			  c != SUSP_CHAR(tty))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 			start_tty(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 			process_echoes(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) n_tty_receive_char_flagged(struct tty_struct *tty, unsigned char c, char flag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 	switch (flag) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 	case TTY_BREAK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 		n_tty_receive_break(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 	case TTY_PARITY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 	case TTY_FRAME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 		n_tty_receive_parity_error(tty, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 	case TTY_OVERRUN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 		n_tty_receive_overrun(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 		tty_err(tty, "unknown flag %d\n", flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) n_tty_receive_char_lnext(struct tty_struct *tty, unsigned char c, char flag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 	struct n_tty_data *ldata = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 	ldata->lnext = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 	if (likely(flag == TTY_NORMAL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 		if (I_ISTRIP(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 			c &= 0x7f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 		if (I_IUCLC(tty) && L_IEXTEN(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 			c = tolower(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 		n_tty_receive_char(tty, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 		n_tty_receive_char_flagged(tty, c, flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) n_tty_receive_buf_real_raw(struct tty_struct *tty, const unsigned char *cp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 			   char *fp, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 	struct n_tty_data *ldata = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 	size_t n, head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 	head = ldata->read_head & (N_TTY_BUF_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 	n = min_t(size_t, count, N_TTY_BUF_SIZE - head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 	memcpy(read_buf_addr(ldata, head), cp, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 	ldata->read_head += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 	cp += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 	count -= n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 	head = ldata->read_head & (N_TTY_BUF_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 	n = min_t(size_t, count, N_TTY_BUF_SIZE - head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 	memcpy(read_buf_addr(ldata, head), cp, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 	ldata->read_head += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) n_tty_receive_buf_raw(struct tty_struct *tty, const unsigned char *cp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 		      char *fp, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 	struct n_tty_data *ldata = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 	char flag = TTY_NORMAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 	while (count--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 		if (fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 			flag = *fp++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 		if (likely(flag == TTY_NORMAL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 			put_tty_queue(*cp++, ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 			n_tty_receive_char_flagged(tty, *cp++, flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) n_tty_receive_buf_closing(struct tty_struct *tty, const unsigned char *cp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 			  char *fp, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 	char flag = TTY_NORMAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 	while (count--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 		if (fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 			flag = *fp++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 		if (likely(flag == TTY_NORMAL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 			n_tty_receive_char_closing(tty, *cp++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) n_tty_receive_buf_standard(struct tty_struct *tty, const unsigned char *cp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 			  char *fp, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 	struct n_tty_data *ldata = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 	char flag = TTY_NORMAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 	while (count--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 		if (fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 			flag = *fp++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 		if (likely(flag == TTY_NORMAL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 			unsigned char c = *cp++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 			if (I_ISTRIP(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 				c &= 0x7f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 			if (I_IUCLC(tty) && L_IEXTEN(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 				c = tolower(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 			if (L_EXTPROC(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 				put_tty_queue(c, ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 			if (!test_bit(c, ldata->char_map))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 				n_tty_receive_char_inline(tty, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 			else if (n_tty_receive_char_special(tty, c) && count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 				if (fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 					flag = *fp++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 				n_tty_receive_char_lnext(tty, *cp++, flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 				count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 			n_tty_receive_char_flagged(tty, *cp++, flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) n_tty_receive_buf_fast(struct tty_struct *tty, const unsigned char *cp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 		       char *fp, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 	struct n_tty_data *ldata = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 	char flag = TTY_NORMAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 	while (count--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 		if (fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 			flag = *fp++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 		if (likely(flag == TTY_NORMAL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 			unsigned char c = *cp++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 			if (!test_bit(c, ldata->char_map))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 				n_tty_receive_char_fast(tty, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 			else if (n_tty_receive_char_special(tty, c) && count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 				if (fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 					flag = *fp++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 				n_tty_receive_char_lnext(tty, *cp++, flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 				count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 			n_tty_receive_char_flagged(tty, *cp++, flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) static void __receive_buf(struct tty_struct *tty, const unsigned char *cp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 			  char *fp, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 	struct n_tty_data *ldata = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 	bool preops = I_ISTRIP(tty) || (I_IUCLC(tty) && L_IEXTEN(tty));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 	if (ldata->real_raw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 		n_tty_receive_buf_real_raw(tty, cp, fp, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 	else if (ldata->raw || (L_EXTPROC(tty) && !preops))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 		n_tty_receive_buf_raw(tty, cp, fp, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 	else if (tty->closing && !L_EXTPROC(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 		n_tty_receive_buf_closing(tty, cp, fp, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 		if (ldata->lnext) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 			char flag = TTY_NORMAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 			if (fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 				flag = *fp++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 			n_tty_receive_char_lnext(tty, *cp++, flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 			count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 		if (!preops && !I_PARMRK(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 			n_tty_receive_buf_fast(tty, cp, fp, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 			n_tty_receive_buf_standard(tty, cp, fp, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 		flush_echoes(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 		if (tty->ops->flush_chars)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 			tty->ops->flush_chars(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 	if (ldata->icanon && !L_EXTPROC(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 	/* publish read_head to consumer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 	smp_store_release(&ldata->commit_head, ldata->read_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 	if (read_cnt(ldata)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 		kill_fasync(&tty->fasync, SIGIO, POLL_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 		wake_up_interruptible_poll(&tty->read_wait, EPOLLIN | EPOLLRDNORM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661)  *	n_tty_receive_buf_common	-	process input
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662)  *	@tty: device to receive input
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663)  *	@cp: input chars
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664)  *	@fp: flags for each char (if NULL, all chars are TTY_NORMAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665)  *	@count: number of input chars in @cp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667)  *	Called by the terminal driver when a block of characters has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668)  *	been received. This function must be called from soft contexts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669)  *	not from interrupt context. The driver is responsible for making
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670)  *	calls one at a time and in order (or using flush_to_ldisc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672)  *	Returns the # of input chars from @cp which were processed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674)  *	In canonical mode, the maximum line length is 4096 chars (including
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675)  *	the line termination char); lines longer than 4096 chars are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676)  *	truncated. After 4095 chars, input data is still processed but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677)  *	not stored. Overflow processing ensures the tty can always
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678)  *	receive more input until at least one line can be read.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680)  *	In non-canonical mode, the read buffer will only accept 4095 chars;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681)  *	this provides the necessary space for a newline char if the input
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682)  *	mode is switched to canonical.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684)  *	Note it is possible for the read buffer to _contain_ 4096 chars
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685)  *	in non-canonical mode: the read buffer could already contain the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686)  *	maximum canon line of 4096 chars when the mode is switched to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687)  *	non-canonical.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689)  *	n_tty_receive_buf()/producer path:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690)  *		claims non-exclusive termios_rwsem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691)  *		publishes commit_head or canon_head
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) n_tty_receive_buf_common(struct tty_struct *tty, const unsigned char *cp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 			 char *fp, int count, int flow)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 	struct n_tty_data *ldata = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 	int room, n, rcvd = 0, overflow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 	down_read(&tty->termios_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 		 * When PARMRK is set, each input char may take up to 3 chars
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 		 * in the read buf; reduce the buffer space avail by 3x
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 		 * If we are doing input canonicalization, and there are no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 		 * pending newlines, let characters through without limit, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 		 * that erase characters will be handled.  Other excess
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 		 * characters will be beeped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 		 * paired with store in *_copy_from_read_buf() -- guarantees
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 		 * the consumer has loaded the data in read_buf up to the new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 		 * read_tail (so this producer will not overwrite unread data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 		size_t tail = smp_load_acquire(&ldata->read_tail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 		room = N_TTY_BUF_SIZE - (ldata->read_head - tail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 		if (I_PARMRK(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 			room = (room + 2) / 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 		room--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 		if (room <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 			overflow = ldata->icanon && ldata->canon_head == tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 			if (overflow && room < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 				ldata->read_head--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 			room = overflow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 			ldata->no_room = flow && !room;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 			overflow = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 		n = min(count, room);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 		if (!n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 		/* ignore parity errors if handling overflow */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 		if (!overflow || !fp || *fp != TTY_PARITY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 			__receive_buf(tty, cp, fp, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 		cp += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 		if (fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 			fp += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 		count -= n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 		rcvd += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 	} while (!test_bit(TTY_LDISC_CHANGING, &tty->flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 	tty->receive_room = room;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 	/* Unthrottle if handling overflow on pty */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 	if (tty->driver->type == TTY_DRIVER_TYPE_PTY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 		if (overflow) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 			tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 			tty_unthrottle_safe(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 			__tty_set_flow_change(tty, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 		n_tty_check_throttle(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 	up_read(&tty->termios_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 	return rcvd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 			      char *fp, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 	n_tty_receive_buf_common(tty, cp, fp, count, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) static int n_tty_receive_buf2(struct tty_struct *tty, const unsigned char *cp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 			      char *fp, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 	return n_tty_receive_buf_common(tty, cp, fp, count, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776)  *	n_tty_set_termios	-	termios data changed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777)  *	@tty: terminal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778)  *	@old: previous data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780)  *	Called by the tty layer when the user changes termios flags so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781)  *	that the line discipline can plan ahead. This function cannot sleep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782)  *	and is protected from re-entry by the tty layer. The user is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783)  *	guaranteed that this function will not be re-entered or in progress
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784)  *	when the ldisc is closed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786)  *	Locking: Caller holds tty->termios_rwsem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 	struct n_tty_data *ldata = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 	if (!old || (old->c_lflag ^ tty->termios.c_lflag) & (ICANON | EXTPROC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 		bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 		ldata->line_start = ldata->read_tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 		if (!L_ICANON(tty) || !read_cnt(ldata)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 			ldata->canon_head = ldata->read_tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 			ldata->push = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 			set_bit((ldata->read_head - 1) & (N_TTY_BUF_SIZE - 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 				ldata->read_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 			ldata->canon_head = ldata->read_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 			ldata->push = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 		ldata->commit_head = ldata->read_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 		ldata->erasing = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 		ldata->lnext = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 	ldata->icanon = (L_ICANON(tty) != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 	if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 	    I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 	    I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 	    I_PARMRK(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 		bitmap_zero(ldata->char_map, 256);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 		if (I_IGNCR(tty) || I_ICRNL(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 			set_bit('\r', ldata->char_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 		if (I_INLCR(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 			set_bit('\n', ldata->char_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 		if (L_ICANON(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 			set_bit(ERASE_CHAR(tty), ldata->char_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 			set_bit(KILL_CHAR(tty), ldata->char_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 			set_bit(EOF_CHAR(tty), ldata->char_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 			set_bit('\n', ldata->char_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 			set_bit(EOL_CHAR(tty), ldata->char_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 			if (L_IEXTEN(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 				set_bit(WERASE_CHAR(tty), ldata->char_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 				set_bit(LNEXT_CHAR(tty), ldata->char_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 				set_bit(EOL2_CHAR(tty), ldata->char_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 				if (L_ECHO(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 					set_bit(REPRINT_CHAR(tty),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 						ldata->char_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 		if (I_IXON(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 			set_bit(START_CHAR(tty), ldata->char_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 			set_bit(STOP_CHAR(tty), ldata->char_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 		if (L_ISIG(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 			set_bit(INTR_CHAR(tty), ldata->char_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 			set_bit(QUIT_CHAR(tty), ldata->char_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 			set_bit(SUSP_CHAR(tty), ldata->char_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 		clear_bit(__DISABLED_CHAR, ldata->char_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 		ldata->raw = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 		ldata->real_raw = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 		ldata->raw = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 		if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 		    (I_IGNPAR(tty) || !I_INPCK(tty)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 		    (tty->driver->flags & TTY_DRIVER_REAL_RAW))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 			ldata->real_raw = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 			ldata->real_raw = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 	 * Fix tty hang when I_IXON(tty) is cleared, but the tty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 	 * been stopped by STOP_CHAR(tty) before it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 	if (!I_IXON(tty) && old && (old->c_iflag & IXON) && !tty->flow_stopped) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 		start_tty(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 		process_echoes(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 	/* The termios change make the tty ready for I/O */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 	wake_up_interruptible(&tty->write_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 	wake_up_interruptible(&tty->read_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874)  *	n_tty_close		-	close the ldisc for this tty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875)  *	@tty: device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877)  *	Called from the terminal layer when this line discipline is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878)  *	being shut down, either because of a close or becsuse of a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879)  *	discipline change. The function will not be called while other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880)  *	ldisc methods are in progress.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) static void n_tty_close(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 	struct n_tty_data *ldata = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 	if (tty->link)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 		n_tty_packet_mode_flush(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) 	vfree(ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 	tty->disc_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895)  *	n_tty_open		-	open an ldisc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896)  *	@tty: terminal to open
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898)  *	Called when this line discipline is being attached to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899)  *	terminal device. Can sleep. Called serialized so that no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900)  *	other events will occur in parallel. No further open will occur
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901)  *	until a close.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) static int n_tty_open(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 	struct n_tty_data *ldata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 	/* Currently a malloc failure here can panic */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 	ldata = vzalloc(sizeof(*ldata));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 	if (!ldata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 	ldata->overrun_time = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 	mutex_init(&ldata->atomic_read_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 	mutex_init(&ldata->output_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 	tty->disc_data = ldata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 	tty->closing = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 	/* indicate buffer work may resume */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 	clear_bit(TTY_LDISC_HALTED, &tty->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) 	n_tty_set_termios(tty, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 	tty_unthrottle(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) static inline int input_available_p(struct tty_struct *tty, int poll)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 	struct n_tty_data *ldata = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) 	int amt = poll && !TIME_CHAR(tty) && MIN_CHAR(tty) ? MIN_CHAR(tty) : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 	if (ldata->icanon && !L_EXTPROC(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) 		return ldata->canon_head != ldata->read_tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) 		return ldata->commit_head - ldata->read_tail >= amt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938)  *	copy_from_read_buf	-	copy read data directly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939)  *	@tty: terminal device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940)  *	@kbp: data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941)  *	@nr: size of data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943)  *	Helper function to speed up n_tty_read.  It is only called when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944)  *	ICANON is off; it copies characters straight from the tty queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946)  *	Called under the ldata->atomic_read_lock sem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948)  *	Returns true if it successfully copied data, but there is still
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949)  *	more data to be had.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951)  *	n_tty_read()/consumer path:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952)  *		caller holds non-exclusive termios_rwsem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953)  *		read_tail published
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) static bool copy_from_read_buf(struct tty_struct *tty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 				      unsigned char **kbp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 				      size_t *nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 	struct n_tty_data *ldata = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 	size_t n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 	bool is_eof;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 	size_t head = smp_load_acquire(&ldata->commit_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 	size_t tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 	n = min(head - ldata->read_tail, N_TTY_BUF_SIZE - tail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 	n = min(*nr, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 	if (n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) 		unsigned char *from = read_buf_addr(ldata, tail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) 		memcpy(*kbp, from, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 		is_eof = n == 1 && *from == EOF_CHAR(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) 		tty_audit_add_data(tty, from, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) 		zero_buffer(tty, from, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 		smp_store_release(&ldata->read_tail, ldata->read_tail + n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) 		/* Turn single EOF into zero-length read */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 		if (L_EXTPROC(tty) && ldata->icanon && is_eof &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 		    (head == ldata->read_tail))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 		*kbp += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 		*nr -= n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 		/* If we have more to copy, let the caller know */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 		return head != ldata->read_tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990)  *	canon_copy_from_read_buf	-	copy read data in canonical mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991)  *	@tty: terminal device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992)  *	@kbp: data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993)  *	@nr: size of data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995)  *	Helper function for n_tty_read.  It is only called when ICANON is on;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996)  *	it copies one line of input up to and including the line-delimiting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997)  *	character into the result buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999)  *	NB: When termios is changed from non-canonical to canonical mode and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000)  *	the read buffer contains data, n_tty_set_termios() simulates an EOF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001)  *	push (as if C-d were input) _without_ the DISABLED_CHAR in the buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002)  *	This causes data already processed as input to be immediately available
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003)  *	as input although a newline has not been received.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005)  *	Called under the atomic_read_lock mutex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007)  *	n_tty_read()/consumer path:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008)  *		caller holds non-exclusive termios_rwsem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009)  *		read_tail published
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) static bool canon_copy_from_read_buf(struct tty_struct *tty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 				     unsigned char **kbp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 				     size_t *nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 	struct n_tty_data *ldata = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 	size_t n, size, more, c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 	size_t eol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 	size_t tail, canon_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 	int found = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 	/* N.B. avoid overrun if nr == 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 	if (!*nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 	canon_head = smp_load_acquire(&ldata->canon_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 	n = min(*nr, canon_head - ldata->read_tail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 	tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 	size = min_t(size_t, tail + n, N_TTY_BUF_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 	n_tty_trace("%s: nr:%zu tail:%zu n:%zu size:%zu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 		    __func__, *nr, tail, n, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 	eol = find_next_bit(ldata->read_flags, size, tail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 	more = n - (size - tail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 	if (eol == N_TTY_BUF_SIZE && more) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 		/* scan wrapped without finding set bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 		eol = find_next_bit(ldata->read_flags, more, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 		found = eol != more;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 		found = eol != size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 	n = eol - tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 	if (n > N_TTY_BUF_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 		n += N_TTY_BUF_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 	c = n + found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 	if (!found || read_buf(ldata, eol) != __DISABLED_CHAR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 		n = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 	n_tty_trace("%s: eol:%zu found:%d n:%zu c:%zu tail:%zu more:%zu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 		    __func__, eol, found, n, c, tail, more);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 	tty_copy(tty, *kbp, tail, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 	*kbp += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) 	*nr -= n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 	if (found)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) 		clear_bit(eol, ldata->read_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 	smp_store_release(&ldata->read_tail, ldata->read_tail + c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) 	if (found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 		if (!ldata->push)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) 			ldata->line_start = ldata->read_tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 			ldata->push = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 		tty_audit_push();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 	/* No EOL found - do a continuation retry if there is more data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 	return ldata->read_tail != canon_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077)  *	job_control		-	check job control
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078)  *	@tty: tty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079)  *	@file: file handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081)  *	Perform job control management checks on this file/tty descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082)  *	and if appropriate send any needed signals and return a negative
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083)  *	error code if action should be taken.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085)  *	Locking: redirected write test is safe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086)  *		 current->signal->tty check is safe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087)  *		 ctrl_lock to safely reference tty->pgrp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) static int job_control(struct tty_struct *tty, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) 	/* Job control check -- must be done at start and after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) 	   every sleep (POSIX.1 7.1.1.4). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) 	/* NOTE: not yet done after every sleep pending a thorough
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) 	   check of the logic of this change. -- jlc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 	/* don't stop on /dev/console */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) 	if (file->f_op->write_iter == redirected_tty_write)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 	return __tty_check_change(tty, SIGTTIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105)  *	n_tty_read		-	read function for tty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106)  *	@tty: tty device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107)  *	@file: file object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108)  *	@buf: userspace buffer pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109)  *	@nr: size of I/O
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111)  *	Perform reads for the line discipline. We are guaranteed that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112)  *	line discipline will not be closed under us but we may get multiple
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113)  *	parallel readers and must handle this ourselves. We may also get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114)  *	a hangup. Always called in user context, may sleep.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116)  *	This code must be sure never to sleep through a hangup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118)  *	n_tty_read()/consumer path:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119)  *		claims non-exclusive termios_rwsem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120)  *		publishes read_tail
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 			  unsigned char *kbuf, size_t nr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 			  void **cookie, unsigned long offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 	struct n_tty_data *ldata = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) 	unsigned char *kb = kbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) 	int c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) 	int minimum, time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) 	ssize_t retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) 	long timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) 	int packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) 	size_t tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) 	 * Is this a continuation of a read started earler?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) 	 * If so, we still hold the atomic_read_lock and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) 	 * termios_rwsem, and can just continue to copy data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) 	if (*cookie) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) 		if (ldata->icanon && !L_EXTPROC(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) 			if (canon_copy_from_read_buf(tty, &kb, &nr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) 				return kb - kbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) 			if (copy_from_read_buf(tty, &kb, &nr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) 				return kb - kbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) 		/* No more data - release locks and stop retries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) 		n_tty_kick_worker(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) 		n_tty_check_unthrottle(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) 		up_read(&tty->termios_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) 		mutex_unlock(&ldata->atomic_read_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) 		*cookie = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) 		return kb - kbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) 	c = job_control(tty, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) 	if (c < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) 		return c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) 	 *	Internal serialization of reads.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) 	if (file->f_flags & O_NONBLOCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) 		if (!mutex_trylock(&ldata->atomic_read_lock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) 			return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) 		if (mutex_lock_interruptible(&ldata->atomic_read_lock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) 			return -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) 	down_read(&tty->termios_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) 	minimum = time = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) 	timeout = MAX_SCHEDULE_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) 	if (!ldata->icanon) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) 		minimum = MIN_CHAR(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) 		if (minimum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) 			time = (HZ / 10) * TIME_CHAR(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) 			timeout = (HZ / 10) * TIME_CHAR(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) 			minimum = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) 	packet = tty->packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) 	tail = ldata->read_tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) 	add_wait_queue(&tty->read_wait, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) 	while (nr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) 		/* First test for status change. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) 		if (packet && tty->link->ctrl_status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) 			unsigned char cs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) 			if (kb != kbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) 			spin_lock_irq(&tty->link->ctrl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) 			cs = tty->link->ctrl_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) 			tty->link->ctrl_status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) 			spin_unlock_irq(&tty->link->ctrl_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) 			*kb++ = cs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) 			nr--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) 		if (!input_available_p(tty, 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) 			up_read(&tty->termios_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) 			tty_buffer_flush_work(tty->port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) 			down_read(&tty->termios_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) 			if (!input_available_p(tty, 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) 				if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) 					retval = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) 				if (tty_hung_up_p(file))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) 				 * Abort readers for ttys which never actually
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) 				 * get hung up.  See __tty_hangup().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) 				if (test_bit(TTY_HUPPING, &tty->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) 				if (!timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) 				if (tty_io_nonblock(tty, file)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) 					retval = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) 				if (signal_pending(current)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) 					retval = -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) 				up_read(&tty->termios_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) 				timeout = wait_woken(&wait, TASK_INTERRUPTIBLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) 						timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) 				down_read(&tty->termios_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) 		if (ldata->icanon && !L_EXTPROC(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) 			if (canon_copy_from_read_buf(tty, &kb, &nr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) 				goto more_to_be_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) 			/* Deal with packet mode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) 			if (packet && kb == kbuf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) 				*kb++ = TIOCPKT_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) 				nr--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) 			 * Copy data, and if there is more to be had
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) 			 * and we have nothing more to wait for, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) 			 * let's mark us for retries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) 			 * NOTE! We return here with both the termios_sem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) 			 * and atomic_read_lock still held, the retries
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) 			 * will release them when done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) 			if (copy_from_read_buf(tty, &kb, &nr) && kb - kbuf >= minimum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) more_to_be_read:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) 				remove_wait_queue(&tty->read_wait, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) 				*cookie = cookie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) 				return kb - kbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) 		n_tty_check_unthrottle(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) 		if (kb - kbuf >= minimum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) 		if (time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) 			timeout = time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) 	if (tail != ldata->read_tail)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) 		n_tty_kick_worker(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) 	up_read(&tty->termios_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) 	remove_wait_queue(&tty->read_wait, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) 	mutex_unlock(&ldata->atomic_read_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) 	if (kb - kbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) 		retval = kb - kbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294)  *	n_tty_write		-	write function for tty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295)  *	@tty: tty device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296)  *	@file: file object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297)  *	@buf: userspace buffer pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298)  *	@nr: size of I/O
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300)  *	Write function of the terminal device.  This is serialized with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301)  *	respect to other write callers but not to termios changes, reads
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302)  *	and other such events.  Since the receive code will echo characters,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303)  *	thus calling driver write methods, the output_lock is used in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304)  *	the output processing functions called here as well as in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305)  *	echo processing function to protect the column state and space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306)  *	left in the buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308)  *	This code must be sure never to sleep through a hangup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310)  *	Locking: output_lock to protect column state and space left
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311)  *		 (note that the process_output*() functions take this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312)  *		  lock themselves)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) 			   const unsigned char *buf, size_t nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) 	const unsigned char *b = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) 	int c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) 	ssize_t retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) 	/* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) 	if (L_TOSTOP(tty) && file->f_op->write_iter != redirected_tty_write) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) 		retval = tty_check_change(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) 		if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) 			return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) 	down_read(&tty->termios_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) 	/* Write out any echoed characters that are still pending */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) 	process_echoes(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) 	add_wait_queue(&tty->write_wait, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) 		if (signal_pending(current)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) 			retval = -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) 		if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) 			retval = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) 		if (O_OPOST(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) 			while (nr > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) 				ssize_t num = process_output_block(tty, b, nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) 				if (num < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) 					if (num == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) 						break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) 					retval = num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) 					goto break_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) 				b += num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) 				nr -= num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) 				if (nr == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) 				c = *b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) 				if (process_output(c, tty) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) 				b++; nr--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) 			if (tty->ops->flush_chars)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) 				tty->ops->flush_chars(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) 			struct n_tty_data *ldata = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) 			while (nr > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) 				mutex_lock(&ldata->output_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) 				c = tty->ops->write(tty, b, nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) 				mutex_unlock(&ldata->output_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) 				if (c < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) 					retval = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) 					goto break_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) 				if (!c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) 				b += c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) 				nr -= c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) 		if (!nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) 		if (tty_io_nonblock(tty, file)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) 			retval = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) 		up_read(&tty->termios_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) 		wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) 		down_read(&tty->termios_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) break_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) 	remove_wait_queue(&tty->write_wait, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) 	if (nr && tty->fasync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) 		set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) 	up_read(&tty->termios_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) 	return (b - buf) ? b - buf : retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403)  *	n_tty_poll		-	poll method for N_TTY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404)  *	@tty: terminal device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405)  *	@file: file accessing it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406)  *	@wait: poll table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408)  *	Called when the line discipline is asked to poll() for data or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409)  *	for special events. This code is not serialized with respect to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410)  *	other events save open/close.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412)  *	This code must be sure never to sleep through a hangup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413)  *	Called without the kernel lock held - fine
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) static __poll_t n_tty_poll(struct tty_struct *tty, struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) 							poll_table *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) 	__poll_t mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) 	poll_wait(file, &tty->read_wait, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) 	poll_wait(file, &tty->write_wait, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) 	if (input_available_p(tty, 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) 		mask |= EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) 		tty_buffer_flush_work(tty->port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) 		if (input_available_p(tty, 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) 			mask |= EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) 	if (tty->packet && tty->link->ctrl_status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) 		mask |= EPOLLPRI | EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) 	if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) 		mask |= EPOLLHUP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) 	if (tty_hung_up_p(file))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) 		mask |= EPOLLHUP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) 	if (tty->ops->write && !tty_is_writelocked(tty) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) 			tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) 			tty_write_room(tty) > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) 		mask |= EPOLLOUT | EPOLLWRNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) 	return mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) static unsigned long inq_canon(struct n_tty_data *ldata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) 	size_t nr, head, tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) 	if (ldata->canon_head == ldata->read_tail)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) 	head = ldata->canon_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) 	tail = ldata->read_tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) 	nr = head - tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) 	/* Skip EOF-chars.. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) 	while (MASK(head) != MASK(tail)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) 		if (test_bit(tail & (N_TTY_BUF_SIZE - 1), ldata->read_flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) 		    read_buf(ldata, tail) == __DISABLED_CHAR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) 			nr--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457) 		tail++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) 	return nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) 		       unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) 	struct n_tty_data *ldata = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) 	case TIOCOUTQ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) 		return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) 	case TIOCINQ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) 		down_write(&tty->termios_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) 		if (L_ICANON(tty) && !L_EXTPROC(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) 			retval = inq_canon(ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) 			retval = read_cnt(ldata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) 		up_write(&tty->termios_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) 		return put_user(retval, (unsigned int __user *) arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) 		return n_tty_ioctl_helper(tty, file, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) static struct tty_ldisc_ops n_tty_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) 	.magic           = TTY_LDISC_MAGIC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) 	.name            = "n_tty",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) 	.open            = n_tty_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) 	.close           = n_tty_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) 	.flush_buffer    = n_tty_flush_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) 	.read            = n_tty_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) 	.write           = n_tty_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) 	.ioctl           = n_tty_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) 	.set_termios     = n_tty_set_termios,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) 	.poll            = n_tty_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) 	.receive_buf     = n_tty_receive_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) 	.write_wakeup    = n_tty_write_wakeup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) 	.receive_buf2	 = n_tty_receive_buf2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501)  *	n_tty_inherit_ops	-	inherit N_TTY methods
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502)  *	@ops: struct tty_ldisc_ops where to save N_TTY methods
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504)  *	Enables a 'subclass' line discipline to 'inherit' N_TTY methods.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) 	*ops = n_tty_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) 	ops->owner = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) 	ops->refcount = ops->flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) EXPORT_SYMBOL_GPL(n_tty_inherit_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) void __init n_tty_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) 	tty_register_ldisc(N_TTY, &n_tty_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) }