162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci#ifndef _LINUX_TTY_PORT_H
362306a36Sopenharmony_ci#define _LINUX_TTY_PORT_H
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci#include <linux/kfifo.h>
662306a36Sopenharmony_ci#include <linux/kref.h>
762306a36Sopenharmony_ci#include <linux/mutex.h>
862306a36Sopenharmony_ci#include <linux/tty_buffer.h>
962306a36Sopenharmony_ci#include <linux/wait.h>
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_cistruct attribute_group;
1262306a36Sopenharmony_cistruct tty_driver;
1362306a36Sopenharmony_cistruct tty_port;
1462306a36Sopenharmony_cistruct tty_struct;
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci/**
1762306a36Sopenharmony_ci * struct tty_port_operations -- operations on tty_port
1862306a36Sopenharmony_ci * @carrier_raised: return true if the carrier is raised on @port
1962306a36Sopenharmony_ci * @dtr_rts: raise the DTR line if @active is true, otherwise lower DTR
2062306a36Sopenharmony_ci * @shutdown: called when the last close completes or a hangup finishes IFF the
2162306a36Sopenharmony_ci *	port was initialized. Do not use to free resources. Turn off the device
2262306a36Sopenharmony_ci *	only. Called under the port mutex to serialize against @activate and
2362306a36Sopenharmony_ci *	@shutdown.
2462306a36Sopenharmony_ci * @activate: called under the port mutex from tty_port_open(), serialized using
2562306a36Sopenharmony_ci *	the port mutex. Supposed to turn on the device.
2662306a36Sopenharmony_ci *
2762306a36Sopenharmony_ci *	FIXME: long term getting the tty argument *out* of this would be good
2862306a36Sopenharmony_ci *	for consoles.
2962306a36Sopenharmony_ci *
3062306a36Sopenharmony_ci * @destruct: called on the final put of a port. Free resources, possibly incl.
3162306a36Sopenharmony_ci *	the port itself.
3262306a36Sopenharmony_ci */
3362306a36Sopenharmony_cistruct tty_port_operations {
3462306a36Sopenharmony_ci	bool (*carrier_raised)(struct tty_port *port);
3562306a36Sopenharmony_ci	void (*dtr_rts)(struct tty_port *port, bool active);
3662306a36Sopenharmony_ci	void (*shutdown)(struct tty_port *port);
3762306a36Sopenharmony_ci	int (*activate)(struct tty_port *port, struct tty_struct *tty);
3862306a36Sopenharmony_ci	void (*destruct)(struct tty_port *port);
3962306a36Sopenharmony_ci};
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_cistruct tty_port_client_operations {
4262306a36Sopenharmony_ci	size_t (*receive_buf)(struct tty_port *port, const u8 *cp, const u8 *fp,
4362306a36Sopenharmony_ci			      size_t count);
4462306a36Sopenharmony_ci	void (*lookahead_buf)(struct tty_port *port, const u8 *cp,
4562306a36Sopenharmony_ci			      const u8 *fp, size_t count);
4662306a36Sopenharmony_ci	void (*write_wakeup)(struct tty_port *port);
4762306a36Sopenharmony_ci};
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ciextern const struct tty_port_client_operations tty_port_default_client_ops;
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci/**
5262306a36Sopenharmony_ci * struct tty_port -- port level information
5362306a36Sopenharmony_ci *
5462306a36Sopenharmony_ci * @buf: buffer for this port, locked internally
5562306a36Sopenharmony_ci * @tty: back pointer to &struct tty_struct, valid only if the tty is open. Use
5662306a36Sopenharmony_ci *	 tty_port_tty_get() to obtain it (and tty_kref_put() to release).
5762306a36Sopenharmony_ci * @itty: internal back pointer to &struct tty_struct. Avoid this. It should be
5862306a36Sopenharmony_ci *	  eliminated in the long term.
5962306a36Sopenharmony_ci * @ops: tty port operations (like activate, shutdown), see &struct
6062306a36Sopenharmony_ci *	 tty_port_operations
6162306a36Sopenharmony_ci * @client_ops: tty port client operations (like receive_buf, write_wakeup).
6262306a36Sopenharmony_ci *		By default, tty_port_default_client_ops is used.
6362306a36Sopenharmony_ci * @lock: lock protecting @tty
6462306a36Sopenharmony_ci * @blocked_open: # of procs waiting for open in tty_port_block_til_ready()
6562306a36Sopenharmony_ci * @count: usage count
6662306a36Sopenharmony_ci * @open_wait: open waiters queue (waiting e.g. for a carrier)
6762306a36Sopenharmony_ci * @delta_msr_wait: modem status change queue (waiting for MSR changes)
6862306a36Sopenharmony_ci * @flags: user TTY flags (%ASYNC_)
6962306a36Sopenharmony_ci * @iflags: internal flags (%TTY_PORT_)
7062306a36Sopenharmony_ci * @console: when set, the port is a console
7162306a36Sopenharmony_ci * @mutex: locking, for open, shutdown and other port operations
7262306a36Sopenharmony_ci * @buf_mutex: @xmit_buf alloc lock
7362306a36Sopenharmony_ci * @xmit_buf: optional xmit buffer used by some drivers
7462306a36Sopenharmony_ci * @xmit_fifo: optional xmit buffer used by some drivers
7562306a36Sopenharmony_ci * @close_delay: delay in jiffies to wait when closing the port
7662306a36Sopenharmony_ci * @closing_wait: delay in jiffies for output to be sent before closing
7762306a36Sopenharmony_ci * @drain_delay: set to zero if no pure time based drain is needed else set to
7862306a36Sopenharmony_ci *		 size of fifo
7962306a36Sopenharmony_ci * @kref: references counter. Reaching zero calls @ops->destruct() if non-%NULL
8062306a36Sopenharmony_ci *	  or frees the port otherwise.
8162306a36Sopenharmony_ci * @client_data: pointer to private data, for @client_ops
8262306a36Sopenharmony_ci *
8362306a36Sopenharmony_ci * Each device keeps its own port level information. &struct tty_port was
8462306a36Sopenharmony_ci * introduced as a common structure for such information. As every TTY device
8562306a36Sopenharmony_ci * shall have a backing tty_port structure, every driver can use these members.
8662306a36Sopenharmony_ci *
8762306a36Sopenharmony_ci * The tty port has a different lifetime to the tty so must be kept apart.
8862306a36Sopenharmony_ci * In addition be careful as tty -> port mappings are valid for the life
8962306a36Sopenharmony_ci * of the tty object but in many cases port -> tty mappings are valid only
9062306a36Sopenharmony_ci * until a hangup so don't use the wrong path.
9162306a36Sopenharmony_ci *
9262306a36Sopenharmony_ci * Tty port shall be initialized by tty_port_init() and shut down either by
9362306a36Sopenharmony_ci * tty_port_destroy() (refcounting not used), or tty_port_put() (refcounting).
9462306a36Sopenharmony_ci *
9562306a36Sopenharmony_ci * There is a lot of helpers around &struct tty_port too. To name the most
9662306a36Sopenharmony_ci * significant ones: tty_port_open(), tty_port_close() (or
9762306a36Sopenharmony_ci * tty_port_close_start() and tty_port_close_end() separately if need be), and
9862306a36Sopenharmony_ci * tty_port_hangup(). These call @ops->activate() and @ops->shutdown() as
9962306a36Sopenharmony_ci * needed.
10062306a36Sopenharmony_ci */
10162306a36Sopenharmony_cistruct tty_port {
10262306a36Sopenharmony_ci	struct tty_bufhead	buf;
10362306a36Sopenharmony_ci	struct tty_struct	*tty;
10462306a36Sopenharmony_ci	struct tty_struct	*itty;
10562306a36Sopenharmony_ci	const struct tty_port_operations *ops;
10662306a36Sopenharmony_ci	const struct tty_port_client_operations *client_ops;
10762306a36Sopenharmony_ci	spinlock_t		lock;
10862306a36Sopenharmony_ci	int			blocked_open;
10962306a36Sopenharmony_ci	int			count;
11062306a36Sopenharmony_ci	wait_queue_head_t	open_wait;
11162306a36Sopenharmony_ci	wait_queue_head_t	delta_msr_wait;
11262306a36Sopenharmony_ci	unsigned long		flags;
11362306a36Sopenharmony_ci	unsigned long		iflags;
11462306a36Sopenharmony_ci	unsigned char		console:1;
11562306a36Sopenharmony_ci	struct mutex		mutex;
11662306a36Sopenharmony_ci	struct mutex		buf_mutex;
11762306a36Sopenharmony_ci	unsigned char		*xmit_buf;
11862306a36Sopenharmony_ci	DECLARE_KFIFO_PTR(xmit_fifo, unsigned char);
11962306a36Sopenharmony_ci	unsigned int		close_delay;
12062306a36Sopenharmony_ci	unsigned int		closing_wait;
12162306a36Sopenharmony_ci	int			drain_delay;
12262306a36Sopenharmony_ci	struct kref		kref;
12362306a36Sopenharmony_ci	void			*client_data;
12462306a36Sopenharmony_ci};
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ci/* tty_port::iflags bits -- use atomic bit ops */
12762306a36Sopenharmony_ci#define TTY_PORT_INITIALIZED	0	/* device is initialized */
12862306a36Sopenharmony_ci#define TTY_PORT_SUSPENDED	1	/* device is suspended */
12962306a36Sopenharmony_ci#define TTY_PORT_ACTIVE		2	/* device is open */
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci/*
13262306a36Sopenharmony_ci * uart drivers: use the uart_port::status field and the UPSTAT_* defines
13362306a36Sopenharmony_ci * for s/w-based flow control steering and carrier detection status
13462306a36Sopenharmony_ci */
13562306a36Sopenharmony_ci#define TTY_PORT_CTS_FLOW	3	/* h/w flow control enabled */
13662306a36Sopenharmony_ci#define TTY_PORT_CHECK_CD	4	/* carrier detect enabled */
13762306a36Sopenharmony_ci#define TTY_PORT_KOPENED	5	/* device exclusively opened by
13862306a36Sopenharmony_ci					   kernel */
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_civoid tty_port_init(struct tty_port *port);
14162306a36Sopenharmony_civoid tty_port_link_device(struct tty_port *port, struct tty_driver *driver,
14262306a36Sopenharmony_ci		unsigned index);
14362306a36Sopenharmony_cistruct device *tty_port_register_device(struct tty_port *port,
14462306a36Sopenharmony_ci		struct tty_driver *driver, unsigned index,
14562306a36Sopenharmony_ci		struct device *device);
14662306a36Sopenharmony_cistruct device *tty_port_register_device_attr(struct tty_port *port,
14762306a36Sopenharmony_ci		struct tty_driver *driver, unsigned index,
14862306a36Sopenharmony_ci		struct device *device, void *drvdata,
14962306a36Sopenharmony_ci		const struct attribute_group **attr_grp);
15062306a36Sopenharmony_cistruct device *tty_port_register_device_serdev(struct tty_port *port,
15162306a36Sopenharmony_ci		struct tty_driver *driver, unsigned index,
15262306a36Sopenharmony_ci		struct device *device);
15362306a36Sopenharmony_cistruct device *tty_port_register_device_attr_serdev(struct tty_port *port,
15462306a36Sopenharmony_ci		struct tty_driver *driver, unsigned index,
15562306a36Sopenharmony_ci		struct device *device, void *drvdata,
15662306a36Sopenharmony_ci		const struct attribute_group **attr_grp);
15762306a36Sopenharmony_civoid tty_port_unregister_device(struct tty_port *port,
15862306a36Sopenharmony_ci		struct tty_driver *driver, unsigned index);
15962306a36Sopenharmony_ciint tty_port_alloc_xmit_buf(struct tty_port *port);
16062306a36Sopenharmony_civoid tty_port_free_xmit_buf(struct tty_port *port);
16162306a36Sopenharmony_civoid tty_port_destroy(struct tty_port *port);
16262306a36Sopenharmony_civoid tty_port_put(struct tty_port *port);
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_cistatic inline struct tty_port *tty_port_get(struct tty_port *port)
16562306a36Sopenharmony_ci{
16662306a36Sopenharmony_ci	if (port && kref_get_unless_zero(&port->kref))
16762306a36Sopenharmony_ci		return port;
16862306a36Sopenharmony_ci	return NULL;
16962306a36Sopenharmony_ci}
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ci/* If the cts flow control is enabled, return true. */
17262306a36Sopenharmony_cistatic inline bool tty_port_cts_enabled(const struct tty_port *port)
17362306a36Sopenharmony_ci{
17462306a36Sopenharmony_ci	return test_bit(TTY_PORT_CTS_FLOW, &port->iflags);
17562306a36Sopenharmony_ci}
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_cistatic inline void tty_port_set_cts_flow(struct tty_port *port, bool val)
17862306a36Sopenharmony_ci{
17962306a36Sopenharmony_ci	assign_bit(TTY_PORT_CTS_FLOW, &port->iflags, val);
18062306a36Sopenharmony_ci}
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_cistatic inline bool tty_port_active(const struct tty_port *port)
18362306a36Sopenharmony_ci{
18462306a36Sopenharmony_ci	return test_bit(TTY_PORT_ACTIVE, &port->iflags);
18562306a36Sopenharmony_ci}
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_cistatic inline void tty_port_set_active(struct tty_port *port, bool val)
18862306a36Sopenharmony_ci{
18962306a36Sopenharmony_ci	assign_bit(TTY_PORT_ACTIVE, &port->iflags, val);
19062306a36Sopenharmony_ci}
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_cistatic inline bool tty_port_check_carrier(const struct tty_port *port)
19362306a36Sopenharmony_ci{
19462306a36Sopenharmony_ci	return test_bit(TTY_PORT_CHECK_CD, &port->iflags);
19562306a36Sopenharmony_ci}
19662306a36Sopenharmony_ci
19762306a36Sopenharmony_cistatic inline void tty_port_set_check_carrier(struct tty_port *port, bool val)
19862306a36Sopenharmony_ci{
19962306a36Sopenharmony_ci	assign_bit(TTY_PORT_CHECK_CD, &port->iflags, val);
20062306a36Sopenharmony_ci}
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_cistatic inline bool tty_port_suspended(const struct tty_port *port)
20362306a36Sopenharmony_ci{
20462306a36Sopenharmony_ci	return test_bit(TTY_PORT_SUSPENDED, &port->iflags);
20562306a36Sopenharmony_ci}
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_cistatic inline void tty_port_set_suspended(struct tty_port *port, bool val)
20862306a36Sopenharmony_ci{
20962306a36Sopenharmony_ci	assign_bit(TTY_PORT_SUSPENDED, &port->iflags, val);
21062306a36Sopenharmony_ci}
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_cistatic inline bool tty_port_initialized(const struct tty_port *port)
21362306a36Sopenharmony_ci{
21462306a36Sopenharmony_ci	return test_bit(TTY_PORT_INITIALIZED, &port->iflags);
21562306a36Sopenharmony_ci}
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_cistatic inline void tty_port_set_initialized(struct tty_port *port, bool val)
21862306a36Sopenharmony_ci{
21962306a36Sopenharmony_ci	assign_bit(TTY_PORT_INITIALIZED, &port->iflags, val);
22062306a36Sopenharmony_ci}
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_cistatic inline bool tty_port_kopened(const struct tty_port *port)
22362306a36Sopenharmony_ci{
22462306a36Sopenharmony_ci	return test_bit(TTY_PORT_KOPENED, &port->iflags);
22562306a36Sopenharmony_ci}
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_cistatic inline void tty_port_set_kopened(struct tty_port *port, bool val)
22862306a36Sopenharmony_ci{
22962306a36Sopenharmony_ci	assign_bit(TTY_PORT_KOPENED, &port->iflags, val);
23062306a36Sopenharmony_ci}
23162306a36Sopenharmony_ci
23262306a36Sopenharmony_cistruct tty_struct *tty_port_tty_get(struct tty_port *port);
23362306a36Sopenharmony_civoid tty_port_tty_set(struct tty_port *port, struct tty_struct *tty);
23462306a36Sopenharmony_cibool tty_port_carrier_raised(struct tty_port *port);
23562306a36Sopenharmony_civoid tty_port_raise_dtr_rts(struct tty_port *port);
23662306a36Sopenharmony_civoid tty_port_lower_dtr_rts(struct tty_port *port);
23762306a36Sopenharmony_civoid tty_port_hangup(struct tty_port *port);
23862306a36Sopenharmony_civoid tty_port_tty_hangup(struct tty_port *port, bool check_clocal);
23962306a36Sopenharmony_civoid tty_port_tty_wakeup(struct tty_port *port);
24062306a36Sopenharmony_ciint tty_port_block_til_ready(struct tty_port *port, struct tty_struct *tty,
24162306a36Sopenharmony_ci		struct file *filp);
24262306a36Sopenharmony_ciint tty_port_close_start(struct tty_port *port, struct tty_struct *tty,
24362306a36Sopenharmony_ci		struct file *filp);
24462306a36Sopenharmony_civoid tty_port_close_end(struct tty_port *port, struct tty_struct *tty);
24562306a36Sopenharmony_civoid tty_port_close(struct tty_port *port, struct tty_struct *tty,
24662306a36Sopenharmony_ci		struct file *filp);
24762306a36Sopenharmony_ciint tty_port_install(struct tty_port *port, struct tty_driver *driver,
24862306a36Sopenharmony_ci		struct tty_struct *tty);
24962306a36Sopenharmony_ciint tty_port_open(struct tty_port *port, struct tty_struct *tty,
25062306a36Sopenharmony_ci		struct file *filp);
25162306a36Sopenharmony_ci
25262306a36Sopenharmony_cistatic inline int tty_port_users(struct tty_port *port)
25362306a36Sopenharmony_ci{
25462306a36Sopenharmony_ci	return port->count + port->blocked_open;
25562306a36Sopenharmony_ci}
25662306a36Sopenharmony_ci
25762306a36Sopenharmony_ci#endif
258