tty_driver.rst 4.6 KB

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