tty_port.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_TTY_PORT_H
  3. #define _LINUX_TTY_PORT_H
  4. #include <linux/kfifo.h>
  5. #include <linux/kref.h>
  6. #include <linux/mutex.h>
  7. #include <linux/tty_buffer.h>
  8. #include <linux/wait.h>
  9. #include <linux/android_kabi.h>
  10. struct attribute_group;
  11. struct tty_driver;
  12. struct tty_port;
  13. struct tty_struct;
  14. /**
  15. * struct tty_port_operations -- operations on tty_port
  16. * @carrier_raised: return 1 if the carrier is raised on @port
  17. * @dtr_rts: raise the DTR line if @raise is nonzero, otherwise lower DTR
  18. * @shutdown: called when the last close completes or a hangup finishes IFF the
  19. * port was initialized. Do not use to free resources. Turn off the device
  20. * only. Called under the port mutex to serialize against @activate and
  21. * @shutdown.
  22. * @activate: called under the port mutex from tty_port_open(), serialized using
  23. * the port mutex. Supposed to turn on the device.
  24. *
  25. * FIXME: long term getting the tty argument *out* of this would be good
  26. * for consoles.
  27. *
  28. * @destruct: called on the final put of a port. Free resources, possibly incl.
  29. * the port itself.
  30. */
  31. struct tty_port_operations {
  32. int (*carrier_raised)(struct tty_port *port);
  33. void (*dtr_rts)(struct tty_port *port, int raise);
  34. void (*shutdown)(struct tty_port *port);
  35. int (*activate)(struct tty_port *port, struct tty_struct *tty);
  36. void (*destruct)(struct tty_port *port);
  37. ANDROID_KABI_RESERVE(1);
  38. };
  39. struct tty_port_client_operations {
  40. int (*receive_buf)(struct tty_port *port, const unsigned char *, const unsigned char *, size_t);
  41. void (*lookahead_buf)(struct tty_port *port, const unsigned char *cp,
  42. const unsigned char *fp, unsigned int count);
  43. void (*write_wakeup)(struct tty_port *port);
  44. };
  45. extern const struct tty_port_client_operations tty_port_default_client_ops;
  46. /**
  47. * struct tty_port -- port level information
  48. *
  49. * @buf: buffer for this port, locked internally
  50. * @tty: back pointer to &struct tty_struct, valid only if the tty is open. Use
  51. * tty_port_tty_get() to obtain it (and tty_kref_put() to release).
  52. * @itty: internal back pointer to &struct tty_struct. Avoid this. It should be
  53. * eliminated in the long term.
  54. * @ops: tty port operations (like activate, shutdown), see &struct
  55. * tty_port_operations
  56. * @client_ops: tty port client operations (like receive_buf, write_wakeup).
  57. * By default, tty_port_default_client_ops is used.
  58. * @lock: lock protecting @tty
  59. * @blocked_open: # of procs waiting for open in tty_port_block_til_ready()
  60. * @count: usage count
  61. * @open_wait: open waiters queue (waiting e.g. for a carrier)
  62. * @delta_msr_wait: modem status change queue (waiting for MSR changes)
  63. * @flags: user TTY flags (%ASYNC_)
  64. * @iflags: internal flags (%TTY_PORT_)
  65. * @console: when set, the port is a console
  66. * @mutex: locking, for open, shutdown and other port operations
  67. * @buf_mutex: @xmit_buf alloc lock
  68. * @xmit_buf: optional xmit buffer used by some drivers
  69. * @xmit_fifo: optional xmit buffer used by some drivers
  70. * @close_delay: delay in jiffies to wait when closing the port
  71. * @closing_wait: delay in jiffies for output to be sent before closing
  72. * @drain_delay: set to zero if no pure time based drain is needed else set to
  73. * size of fifo
  74. * @kref: references counter. Reaching zero calls @ops->destruct() if non-%NULL
  75. * or frees the port otherwise.
  76. * @client_data: pointer to private data, for @client_ops
  77. *
  78. * Each device keeps its own port level information. &struct tty_port was
  79. * introduced as a common structure for such information. As every TTY device
  80. * shall have a backing tty_port structure, every driver can use these members.
  81. *
  82. * The tty port has a different lifetime to the tty so must be kept apart.
  83. * In addition be careful as tty -> port mappings are valid for the life
  84. * of the tty object but in many cases port -> tty mappings are valid only
  85. * until a hangup so don't use the wrong path.
  86. *
  87. * Tty port shall be initialized by tty_port_init() and shut down either by
  88. * tty_port_destroy() (refcounting not used), or tty_port_put() (refcounting).
  89. *
  90. * There is a lot of helpers around &struct tty_port too. To name the most
  91. * significant ones: tty_port_open(), tty_port_close() (or
  92. * tty_port_close_start() and tty_port_close_end() separately if need be), and
  93. * tty_port_hangup(). These call @ops->activate() and @ops->shutdown() as
  94. * needed.
  95. */
  96. struct tty_port {
  97. struct tty_bufhead buf;
  98. struct tty_struct *tty;
  99. struct tty_struct *itty;
  100. const struct tty_port_operations *ops;
  101. const struct tty_port_client_operations *client_ops;
  102. spinlock_t lock;
  103. int blocked_open;
  104. int count;
  105. wait_queue_head_t open_wait;
  106. wait_queue_head_t delta_msr_wait;
  107. unsigned long flags;
  108. unsigned long iflags;
  109. unsigned char console:1;
  110. struct mutex mutex;
  111. struct mutex buf_mutex;
  112. unsigned char *xmit_buf;
  113. DECLARE_KFIFO_PTR(xmit_fifo, unsigned char);
  114. unsigned int close_delay;
  115. unsigned int closing_wait;
  116. int drain_delay;
  117. struct kref kref;
  118. void *client_data;
  119. ANDROID_KABI_RESERVE(1);
  120. };
  121. /* tty_port::iflags bits -- use atomic bit ops */
  122. #define TTY_PORT_INITIALIZED 0 /* device is initialized */
  123. #define TTY_PORT_SUSPENDED 1 /* device is suspended */
  124. #define TTY_PORT_ACTIVE 2 /* device is open */
  125. /*
  126. * uart drivers: use the uart_port::status field and the UPSTAT_* defines
  127. * for s/w-based flow control steering and carrier detection status
  128. */
  129. #define TTY_PORT_CTS_FLOW 3 /* h/w flow control enabled */
  130. #define TTY_PORT_CHECK_CD 4 /* carrier detect enabled */
  131. #define TTY_PORT_KOPENED 5 /* device exclusively opened by
  132. kernel */
  133. void tty_port_init(struct tty_port *port);
  134. void tty_port_link_device(struct tty_port *port, struct tty_driver *driver,
  135. unsigned index);
  136. struct device *tty_port_register_device(struct tty_port *port,
  137. struct tty_driver *driver, unsigned index,
  138. struct device *device);
  139. struct device *tty_port_register_device_attr(struct tty_port *port,
  140. struct tty_driver *driver, unsigned index,
  141. struct device *device, void *drvdata,
  142. const struct attribute_group **attr_grp);
  143. struct device *tty_port_register_device_serdev(struct tty_port *port,
  144. struct tty_driver *driver, unsigned index,
  145. struct device *device);
  146. struct device *tty_port_register_device_attr_serdev(struct tty_port *port,
  147. struct tty_driver *driver, unsigned index,
  148. struct device *device, void *drvdata,
  149. const struct attribute_group **attr_grp);
  150. void tty_port_unregister_device(struct tty_port *port,
  151. struct tty_driver *driver, unsigned index);
  152. int tty_port_alloc_xmit_buf(struct tty_port *port);
  153. void tty_port_free_xmit_buf(struct tty_port *port);
  154. void tty_port_destroy(struct tty_port *port);
  155. void tty_port_put(struct tty_port *port);
  156. static inline struct tty_port *tty_port_get(struct tty_port *port)
  157. {
  158. if (port && kref_get_unless_zero(&port->kref))
  159. return port;
  160. return NULL;
  161. }
  162. /* If the cts flow control is enabled, return true. */
  163. static inline bool tty_port_cts_enabled(const struct tty_port *port)
  164. {
  165. return test_bit(TTY_PORT_CTS_FLOW, &port->iflags);
  166. }
  167. static inline void tty_port_set_cts_flow(struct tty_port *port, bool val)
  168. {
  169. assign_bit(TTY_PORT_CTS_FLOW, &port->iflags, val);
  170. }
  171. static inline bool tty_port_active(const struct tty_port *port)
  172. {
  173. return test_bit(TTY_PORT_ACTIVE, &port->iflags);
  174. }
  175. static inline void tty_port_set_active(struct tty_port *port, bool val)
  176. {
  177. assign_bit(TTY_PORT_ACTIVE, &port->iflags, val);
  178. }
  179. static inline bool tty_port_check_carrier(const struct tty_port *port)
  180. {
  181. return test_bit(TTY_PORT_CHECK_CD, &port->iflags);
  182. }
  183. static inline void tty_port_set_check_carrier(struct tty_port *port, bool val)
  184. {
  185. assign_bit(TTY_PORT_CHECK_CD, &port->iflags, val);
  186. }
  187. static inline bool tty_port_suspended(const struct tty_port *port)
  188. {
  189. return test_bit(TTY_PORT_SUSPENDED, &port->iflags);
  190. }
  191. static inline void tty_port_set_suspended(struct tty_port *port, bool val)
  192. {
  193. assign_bit(TTY_PORT_SUSPENDED, &port->iflags, val);
  194. }
  195. static inline bool tty_port_initialized(const struct tty_port *port)
  196. {
  197. return test_bit(TTY_PORT_INITIALIZED, &port->iflags);
  198. }
  199. static inline void tty_port_set_initialized(struct tty_port *port, bool val)
  200. {
  201. assign_bit(TTY_PORT_INITIALIZED, &port->iflags, val);
  202. }
  203. static inline bool tty_port_kopened(const struct tty_port *port)
  204. {
  205. return test_bit(TTY_PORT_KOPENED, &port->iflags);
  206. }
  207. static inline void tty_port_set_kopened(struct tty_port *port, bool val)
  208. {
  209. assign_bit(TTY_PORT_KOPENED, &port->iflags, val);
  210. }
  211. struct tty_struct *tty_port_tty_get(struct tty_port *port);
  212. void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty);
  213. int tty_port_carrier_raised(struct tty_port *port);
  214. void tty_port_raise_dtr_rts(struct tty_port *port);
  215. void tty_port_lower_dtr_rts(struct tty_port *port);
  216. void tty_port_hangup(struct tty_port *port);
  217. void tty_port_tty_hangup(struct tty_port *port, bool check_clocal);
  218. void tty_port_tty_wakeup(struct tty_port *port);
  219. int tty_port_block_til_ready(struct tty_port *port, struct tty_struct *tty,
  220. struct file *filp);
  221. int tty_port_close_start(struct tty_port *port, struct tty_struct *tty,
  222. struct file *filp);
  223. void tty_port_close_end(struct tty_port *port, struct tty_struct *tty);
  224. void tty_port_close(struct tty_port *port, struct tty_struct *tty,
  225. struct file *filp);
  226. int tty_port_install(struct tty_port *port, struct tty_driver *driver,
  227. struct tty_struct *tty);
  228. int tty_port_open(struct tty_port *port, struct tty_struct *tty,
  229. struct file *filp);
  230. static inline int tty_port_users(struct tty_port *port)
  231. {
  232. return port->count + port->blocked_open;
  233. }
  234. #endif