tty_ldisc.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_TTY_LDISC_H
  3. #define _LINUX_TTY_LDISC_H
  4. struct tty_struct;
  5. #include <linux/fs.h>
  6. #include <linux/wait.h>
  7. #include <linux/atomic.h>
  8. #include <linux/list.h>
  9. #include <linux/lockdep.h>
  10. #include <linux/seq_file.h>
  11. #include <linux/android_kabi.h>
  12. /*
  13. * the semaphore definition
  14. */
  15. struct ld_semaphore {
  16. atomic_long_t count;
  17. raw_spinlock_t wait_lock;
  18. unsigned int wait_readers;
  19. struct list_head read_wait;
  20. struct list_head write_wait;
  21. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  22. struct lockdep_map dep_map;
  23. #endif
  24. };
  25. void __init_ldsem(struct ld_semaphore *sem, const char *name,
  26. struct lock_class_key *key);
  27. #define init_ldsem(sem) \
  28. do { \
  29. static struct lock_class_key __key; \
  30. \
  31. __init_ldsem((sem), #sem, &__key); \
  32. } while (0)
  33. int ldsem_down_read(struct ld_semaphore *sem, long timeout);
  34. int ldsem_down_read_trylock(struct ld_semaphore *sem);
  35. int ldsem_down_write(struct ld_semaphore *sem, long timeout);
  36. int ldsem_down_write_trylock(struct ld_semaphore *sem);
  37. void ldsem_up_read(struct ld_semaphore *sem);
  38. void ldsem_up_write(struct ld_semaphore *sem);
  39. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  40. int ldsem_down_read_nested(struct ld_semaphore *sem, int subclass,
  41. long timeout);
  42. int ldsem_down_write_nested(struct ld_semaphore *sem, int subclass,
  43. long timeout);
  44. #else
  45. # define ldsem_down_read_nested(sem, subclass, timeout) \
  46. ldsem_down_read(sem, timeout)
  47. # define ldsem_down_write_nested(sem, subclass, timeout) \
  48. ldsem_down_write(sem, timeout)
  49. #endif
  50. /**
  51. * struct tty_ldisc_ops - ldisc operations
  52. *
  53. * @name: name of this ldisc rendered in /proc/tty/ldiscs
  54. * @num: ``N_*`` number (%N_TTY, %N_HDLC, ...) reserved to this ldisc
  55. *
  56. * @open: [TTY] ``int ()(struct tty_struct *tty)``
  57. *
  58. * This function is called when the line discipline is associated with the
  59. * @tty. No other call into the line discipline for this tty will occur
  60. * until it completes successfully. It should initialize any state needed
  61. * by the ldisc, and set @tty->receive_room to the maximum amount of data
  62. * the line discipline is willing to accept from the driver with a single
  63. * call to @receive_buf(). Returning an error will prevent the ldisc from
  64. * being attached.
  65. *
  66. * Can sleep.
  67. *
  68. * @close: [TTY] ``void ()(struct tty_struct *tty)``
  69. *
  70. * This function is called when the line discipline is being shutdown,
  71. * either because the @tty is being closed or because the @tty is being
  72. * changed to use a new line discipline. At the point of execution no
  73. * further users will enter the ldisc code for this tty.
  74. *
  75. * Can sleep.
  76. *
  77. * @flush_buffer: [TTY] ``void ()(struct tty_struct *tty)``
  78. *
  79. * This function instructs the line discipline to clear its buffers of any
  80. * input characters it may have queued to be delivered to the user mode
  81. * process. It may be called at any point between open and close.
  82. *
  83. * @read: [TTY] ``ssize_t ()(struct tty_struct *tty, struct file *file,
  84. * unsigned char *buf, size_t nr)``
  85. *
  86. * This function is called when the user requests to read from the @tty.
  87. * The line discipline will return whatever characters it has buffered up
  88. * for the user. If this function is not defined, the user will receive
  89. * an %EIO error. Multiple read calls may occur in parallel and the ldisc
  90. * must deal with serialization issues.
  91. *
  92. * Can sleep.
  93. *
  94. * @write: [TTY] ``ssize_t ()(struct tty_struct *tty, struct file *file,
  95. * const unsigned char *buf, size_t nr)``
  96. *
  97. * This function is called when the user requests to write to the @tty.
  98. * The line discipline will deliver the characters to the low-level tty
  99. * device for transmission, optionally performing some processing on the
  100. * characters first. If this function is not defined, the user will
  101. * receive an %EIO error.
  102. *
  103. * Can sleep.
  104. *
  105. * @ioctl: [TTY] ``int ()(struct tty_struct *tty, unsigned int cmd,
  106. * unsigned long arg)``
  107. *
  108. * This function is called when the user requests an ioctl which is not
  109. * handled by the tty layer or the low-level tty driver. It is intended
  110. * for ioctls which affect line discpline operation. Note that the search
  111. * order for ioctls is (1) tty layer, (2) tty low-level driver, (3) line
  112. * discpline. So a low-level driver can "grab" an ioctl request before
  113. * the line discpline has a chance to see it.
  114. *
  115. * @compat_ioctl: [TTY] ``int ()(struct tty_struct *tty, unsigned int cmd,
  116. * unsigned long arg)``
  117. *
  118. * Process ioctl calls from 32-bit process on 64-bit system.
  119. *
  120. * Note that only ioctls that are neither "pointer to compatible
  121. * structure" nor tty-generic. Something private that takes an integer or
  122. * a pointer to wordsize-sensitive structure belongs here, but most of
  123. * ldiscs will happily leave it %NULL.
  124. *
  125. * @set_termios: [TTY] ``void ()(struct tty_struct *tty, const struct ktermios *old)``
  126. *
  127. * This function notifies the line discpline that a change has been made
  128. * to the termios structure.
  129. *
  130. * @poll: [TTY] ``int ()(struct tty_struct *tty, struct file *file,
  131. * struct poll_table_struct *wait)``
  132. *
  133. * This function is called when a user attempts to select/poll on a @tty
  134. * device. It is solely the responsibility of the line discipline to
  135. * handle poll requests.
  136. *
  137. * @hangup: [TTY] ``void ()(struct tty_struct *tty)``
  138. *
  139. * Called on a hangup. Tells the discipline that it should cease I/O to
  140. * the tty driver. The driver should seek to perform this action quickly
  141. * but should wait until any pending driver I/O is completed. No further
  142. * calls into the ldisc code will occur.
  143. *
  144. * Can sleep.
  145. *
  146. * @receive_buf: [DRV] ``void ()(struct tty_struct *tty,
  147. * const unsigned char *cp, const char *fp, int count)``
  148. *
  149. * This function is called by the low-level tty driver to send characters
  150. * received by the hardware to the line discpline for processing. @cp is
  151. * a pointer to the buffer of input character received by the device. @fp
  152. * is a pointer to an array of flag bytes which indicate whether a
  153. * character was received with a parity error, etc. @fp may be %NULL to
  154. * indicate all data received is %TTY_NORMAL.
  155. *
  156. * @write_wakeup: [DRV] ``void ()(struct tty_struct *tty)``
  157. *
  158. * This function is called by the low-level tty driver to signal that line
  159. * discpline should try to send more characters to the low-level driver
  160. * for transmission. If the line discpline does not have any more data to
  161. * send, it can just return. If the line discipline does have some data to
  162. * send, please arise a tasklet or workqueue to do the real data transfer.
  163. * Do not send data in this hook, it may lead to a deadlock.
  164. *
  165. * @dcd_change: [DRV] ``void ()(struct tty_struct *tty, unsigned int status)``
  166. *
  167. * Tells the discipline that the DCD pin has changed its status. Used
  168. * exclusively by the %N_PPS (Pulse-Per-Second) line discipline.
  169. *
  170. * @receive_buf2: [DRV] ``int ()(struct tty_struct *tty,
  171. * const unsigned char *cp, const char *fp, int count)``
  172. *
  173. * This function is called by the low-level tty driver to send characters
  174. * received by the hardware to the line discpline for processing. @cp is a
  175. * pointer to the buffer of input character received by the device. @fp
  176. * is a pointer to an array of flag bytes which indicate whether a
  177. * character was received with a parity error, etc. @fp may be %NULL to
  178. * indicate all data received is %TTY_NORMAL. If assigned, prefer this
  179. * function for automatic flow control.
  180. *
  181. * @lookahead_buf: [DRV] ``void ()(struct tty_struct *tty,
  182. * const unsigned char *cp, const char *fp, int count)``
  183. *
  184. * This function is called by the low-level tty driver for characters
  185. * not eaten by ->receive_buf() or ->receive_buf2(). It is useful for
  186. * processing high-priority characters such as software flow-control
  187. * characters that could otherwise get stuck into the intermediate
  188. * buffer until tty has room to receive them. Ldisc must be able to
  189. * handle later a ->receive_buf() or ->receive_buf2() call for the
  190. * same characters (e.g. by skipping the actions for high-priority
  191. * characters already handled by ->lookahead_buf()).
  192. *
  193. * @owner: module containting this ldisc (for reference counting)
  194. *
  195. * This structure defines the interface between the tty line discipline
  196. * implementation and the tty routines. The above routines can be defined.
  197. * Unless noted otherwise, they are optional, and can be filled in with a %NULL
  198. * pointer.
  199. *
  200. * Hooks marked [TTY] are invoked from the TTY core, the [DRV] ones from the
  201. * tty_driver side.
  202. */
  203. struct tty_ldisc_ops {
  204. char *name;
  205. int num;
  206. /*
  207. * The following routines are called from above.
  208. */
  209. int (*open)(struct tty_struct *tty);
  210. void (*close)(struct tty_struct *tty);
  211. void (*flush_buffer)(struct tty_struct *tty);
  212. ssize_t (*read)(struct tty_struct *tty, struct file *file,
  213. unsigned char *buf, size_t nr,
  214. void **cookie, unsigned long offset);
  215. ssize_t (*write)(struct tty_struct *tty, struct file *file,
  216. const unsigned char *buf, size_t nr);
  217. int (*ioctl)(struct tty_struct *tty, unsigned int cmd,
  218. unsigned long arg);
  219. int (*compat_ioctl)(struct tty_struct *tty, unsigned int cmd,
  220. unsigned long arg);
  221. void (*set_termios)(struct tty_struct *tty, const struct ktermios *old);
  222. __poll_t (*poll)(struct tty_struct *tty, struct file *file,
  223. struct poll_table_struct *wait);
  224. void (*hangup)(struct tty_struct *tty);
  225. /*
  226. * The following routines are called from below.
  227. */
  228. void (*receive_buf)(struct tty_struct *tty, const unsigned char *cp,
  229. const char *fp, int count);
  230. void (*write_wakeup)(struct tty_struct *tty);
  231. void (*dcd_change)(struct tty_struct *tty, unsigned int status);
  232. int (*receive_buf2)(struct tty_struct *tty, const unsigned char *cp,
  233. const char *fp, int count);
  234. void (*lookahead_buf)(struct tty_struct *tty, const unsigned char *cp,
  235. const unsigned char *fp, unsigned int count);
  236. struct module *owner;
  237. ANDROID_KABI_RESERVE(1);
  238. ANDROID_KABI_RESERVE(2);
  239. };
  240. struct tty_ldisc {
  241. struct tty_ldisc_ops *ops;
  242. struct tty_struct *tty;
  243. };
  244. #define MODULE_ALIAS_LDISC(ldisc) \
  245. MODULE_ALIAS("tty-ldisc-" __stringify(ldisc))
  246. extern const struct seq_operations tty_ldiscs_seq_ops;
  247. struct tty_ldisc *tty_ldisc_ref(struct tty_struct *);
  248. void tty_ldisc_deref(struct tty_ldisc *);
  249. struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *);
  250. void tty_ldisc_flush(struct tty_struct *tty);
  251. int tty_register_ldisc(struct tty_ldisc_ops *new_ldisc);
  252. void tty_unregister_ldisc(struct tty_ldisc_ops *ldisc);
  253. int tty_set_ldisc(struct tty_struct *tty, int disc);
  254. #endif /* _LINUX_TTY_LDISC_H */