162306a36Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci=============================
462306a36Sopenharmony_ciTTY Driver and TTY Operations
562306a36Sopenharmony_ci=============================
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci.. contents:: :local:
862306a36Sopenharmony_ci
962306a36Sopenharmony_ciAllocation
1062306a36Sopenharmony_ci==========
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ciThe first thing a driver needs to do is to allocate a struct tty_driver. This
1362306a36Sopenharmony_ciis done by tty_alloc_driver() (or __tty_alloc_driver()). Next, the newly
1462306a36Sopenharmony_ciallocated structure is filled with information. See `TTY Driver Reference`_ at
1562306a36Sopenharmony_cithe end of this document on what actually shall be filled in.
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ciThe allocation routines expect a number of devices the driver can handle at
1862306a36Sopenharmony_cimost and flags. Flags are those starting ``TTY_DRIVER_`` listed and described
1962306a36Sopenharmony_ciin `TTY Driver Flags`_ below.
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ciWhen the driver is about to be freed, tty_driver_kref_put() is called on that.
2262306a36Sopenharmony_ciIt will decrements the reference count and if it reaches zero, the driver is
2362306a36Sopenharmony_cifreed.
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ciFor reference, both allocation and deallocation functions are explained here in
2662306a36Sopenharmony_cidetail:
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci.. kernel-doc:: drivers/tty/tty_io.c
2962306a36Sopenharmony_ci   :identifiers: __tty_alloc_driver tty_driver_kref_put
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ciTTY Driver Flags
3262306a36Sopenharmony_ci----------------
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ciHere comes the documentation of flags accepted by tty_alloc_driver() (or
3562306a36Sopenharmony_ci__tty_alloc_driver()):
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci.. kernel-doc:: include/linux/tty_driver.h
3862306a36Sopenharmony_ci   :doc: TTY Driver Flags
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci----
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ciRegistration
4362306a36Sopenharmony_ci============
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ciWhen a struct tty_driver is allocated and filled in, it can be registered using
4662306a36Sopenharmony_citty_register_driver(). It is recommended to pass ``TTY_DRIVER_DYNAMIC_DEV`` in
4762306a36Sopenharmony_ciflags of tty_alloc_driver(). If it is not passed, *all* devices are also
4862306a36Sopenharmony_ciregistered during tty_register_driver() and the following paragraph of
4962306a36Sopenharmony_ciregistering devices can be skipped for such drivers. However, the struct
5062306a36Sopenharmony_citty_port part in `Registering Devices`_ is still relevant there.
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci.. kernel-doc:: drivers/tty/tty_io.c
5362306a36Sopenharmony_ci   :identifiers: tty_register_driver tty_unregister_driver
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ciRegistering Devices
5662306a36Sopenharmony_ci-------------------
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ciEvery TTY device shall be backed by a struct tty_port. Usually, TTY drivers
5962306a36Sopenharmony_ciembed tty_port into device's private structures. Further details about handling
6062306a36Sopenharmony_citty_port can be found in :doc:`tty_port`. The driver is also recommended to use
6162306a36Sopenharmony_citty_port's reference counting by tty_port_get() and tty_port_put(). The final
6262306a36Sopenharmony_ciput is supposed to free the tty_port including the device's private struct.
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ciUnless ``TTY_DRIVER_DYNAMIC_DEV`` was passed as flags to tty_alloc_driver(),
6562306a36Sopenharmony_ciTTY driver is supposed to register every device discovered in the system
6662306a36Sopenharmony_ci(the latter is preferred). This is performed by tty_register_device(). Or by
6762306a36Sopenharmony_citty_register_device_attr() if the driver wants to expose some information
6862306a36Sopenharmony_cithrough struct attribute_group. Both of them register ``index``'th device and
6962306a36Sopenharmony_ciupon return, the device can be opened. There are also preferred tty_port
7062306a36Sopenharmony_civariants described in `Linking Devices to Ports`_ later. It is up to driver to
7162306a36Sopenharmony_cimanage free indices and choosing the right one. The TTY layer only refuses to
7262306a36Sopenharmony_ciregister more devices than passed to tty_alloc_driver().
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ciWhen the device is opened, the TTY layer allocates struct tty_struct and starts
7562306a36Sopenharmony_cicalling operations from :c:member:`tty_driver.ops`, see `TTY Operations
7662306a36Sopenharmony_ciReference`_.
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ciThe registration routines are documented as follows:
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci.. kernel-doc:: drivers/tty/tty_io.c
8162306a36Sopenharmony_ci   :identifiers: tty_register_device tty_register_device_attr
8262306a36Sopenharmony_ci        tty_unregister_device
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci----
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ciLinking Devices to Ports
8762306a36Sopenharmony_ci------------------------
8862306a36Sopenharmony_ciAs stated earlier, every TTY device shall have a struct tty_port assigned to
8962306a36Sopenharmony_ciit. It must be known to the TTY layer at :c:member:`tty_driver.ops.install()`
9062306a36Sopenharmony_ciat latest.  There are few helpers to *link* the two. Ideally, the driver uses
9162306a36Sopenharmony_citty_port_register_device() or tty_port_register_device_attr() instead of
9262306a36Sopenharmony_citty_register_device() and tty_register_device_attr() at the registration time.
9362306a36Sopenharmony_ciThis way, the driver needs not care about linking later on.
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ciIf that is not possible, the driver still can link the tty_port to a specific
9662306a36Sopenharmony_ciindex *before* the actual registration by tty_port_link_device(). If it still
9762306a36Sopenharmony_cidoes not fit, tty_port_install() can be used from the
9862306a36Sopenharmony_ci:c:member:`tty_driver.ops.install` hook as a last resort. The last one is
9962306a36Sopenharmony_cidedicated mostly for in-memory devices like PTY where tty_ports are allocated
10062306a36Sopenharmony_cion demand.
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ciThe linking routines are documented here:
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci.. kernel-doc::  drivers/tty/tty_port.c
10562306a36Sopenharmony_ci   :identifiers: tty_port_link_device tty_port_register_device
10662306a36Sopenharmony_ci        tty_port_register_device_attr
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci----
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ciTTY Driver Reference
11162306a36Sopenharmony_ci====================
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ciAll members of struct tty_driver are documented here. The required members are
11462306a36Sopenharmony_cinoted at the end. struct tty_operations are documented next.
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci.. kernel-doc:: include/linux/tty_driver.h
11762306a36Sopenharmony_ci   :identifiers: tty_driver
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci----
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ciTTY Operations Reference
12262306a36Sopenharmony_ci========================
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ciWhen a TTY is registered, these driver hooks can be invoked by the TTY layer:
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ci.. kernel-doc:: include/linux/tty_driver.h
12762306a36Sopenharmony_ci   :identifiers: tty_operations
12862306a36Sopenharmony_ci
129