tty_driver.rst 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. .. SPDX-License-Identifier: GPL-2.0
  2. =============================
  3. TTY Driver and TTY Operations
  4. =============================
  5. .. contents:: :local:
  6. Allocation
  7. ==========
  8. The first thing a driver needs to do is to allocate a struct tty_driver. This
  9. is done by tty_alloc_driver() (or __tty_alloc_driver()). Next, the newly
  10. allocated structure is filled with information. See `TTY Driver Reference`_ at
  11. the end of this document on what actually shall be filled in.
  12. The allocation routines expect a number of devices the driver can handle at
  13. most and flags. Flags are those starting ``TTY_DRIVER_`` listed and described
  14. in `TTY Driver Flags`_ below.
  15. When the driver is about to be freed, tty_driver_kref_put() is called on that.
  16. It will decrements the reference count and if it reaches zero, the driver is
  17. freed.
  18. For reference, both allocation and deallocation functions are explained here in
  19. detail:
  20. .. kernel-doc:: include/linux/tty_driver.h
  21. :identifiers: tty_alloc_driver
  22. .. kernel-doc:: drivers/tty/tty_io.c
  23. :identifiers: __tty_alloc_driver tty_driver_kref_put
  24. TTY Driver Flags
  25. ----------------
  26. Here comes the documentation of flags accepted by tty_alloc_driver() (or
  27. __tty_alloc_driver()):
  28. .. kernel-doc:: include/linux/tty_driver.h
  29. :identifiers: tty_driver_flag
  30. ----
  31. Registration
  32. ============
  33. When a struct tty_driver is allocated and filled in, it can be registered using
  34. tty_register_driver(). It is recommended to pass ``TTY_DRIVER_DYNAMIC_DEV`` in
  35. flags of tty_alloc_driver(). If it is not passed, *all* devices are also
  36. registered during tty_register_driver() and the following paragraph of
  37. registering devices can be skipped for such drivers. However, the struct
  38. tty_port part in `Registering Devices`_ is still relevant there.
  39. .. kernel-doc:: drivers/tty/tty_io.c
  40. :identifiers: tty_register_driver tty_unregister_driver
  41. Registering Devices
  42. -------------------
  43. Every TTY device shall be backed by a struct tty_port. Usually, TTY drivers
  44. embed tty_port into device's private structures. Further details about handling
  45. tty_port can be found in :doc:`tty_port`. The driver is also recommended to use
  46. tty_port's reference counting by tty_port_get() and tty_port_put(). The final
  47. put is supposed to free the tty_port including the device's private struct.
  48. Unless ``TTY_DRIVER_DYNAMIC_DEV`` was passed as flags to tty_alloc_driver(),
  49. TTY driver is supposed to register every device discovered in the system
  50. (the latter is preferred). This is performed by tty_register_device(). Or by
  51. tty_register_device_attr() if the driver wants to expose some information
  52. through struct attribute_group. Both of them register ``index``'th device and
  53. upon return, the device can be opened. There are also preferred tty_port
  54. variants described in `Linking Devices to Ports`_ later. It is up to driver to
  55. manage free indices and choosing the right one. The TTY layer only refuses to
  56. register more devices than passed to tty_alloc_driver().
  57. When the device is opened, the TTY layer allocates struct tty_struct and starts
  58. calling operations from :c:member:`tty_driver.ops`, see `TTY Operations
  59. Reference`_.
  60. The registration routines are documented as follows:
  61. .. kernel-doc:: drivers/tty/tty_io.c
  62. :identifiers: tty_register_device tty_register_device_attr
  63. tty_unregister_device
  64. ----
  65. Linking Devices to Ports
  66. ------------------------
  67. As stated earlier, every TTY device shall have a struct tty_port assigned to
  68. it. It must be known to the TTY layer at :c:member:`tty_driver.ops.install()`
  69. at latest. There are few helpers to *link* the two. Ideally, the driver uses
  70. tty_port_register_device() or tty_port_register_device_attr() instead of
  71. tty_register_device() and tty_register_device_attr() at the registration time.
  72. This way, the driver needs not care about linking later on.
  73. If that is not possible, the driver still can link the tty_port to a specific
  74. index *before* the actual registration by tty_port_link_device(). If it still
  75. does not fit, tty_port_install() can be used from the
  76. :c:member:`tty_driver.ops.install` hook as a last resort. The last one is
  77. dedicated mostly for in-memory devices like PTY where tty_ports are allocated
  78. on demand.
  79. The linking routines are documented here:
  80. .. kernel-doc:: drivers/tty/tty_port.c
  81. :identifiers: tty_port_link_device tty_port_register_device
  82. tty_port_register_device_attr
  83. ----
  84. TTY Driver Reference
  85. ====================
  86. All members of struct tty_driver are documented here. The required members are
  87. noted at the end. struct tty_operations are documented next.
  88. .. kernel-doc:: include/linux/tty_driver.h
  89. :identifiers: tty_driver
  90. ----
  91. TTY Operations Reference
  92. ========================
  93. When a TTY is registered, these driver hooks can be invoked by the TTY layer:
  94. .. kernel-doc:: include/linux/tty_driver.h
  95. :identifiers: tty_operations