tty_driver.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. #ifndef _LINUX_TTY_DRIVER_H
  2. #define _LINUX_TTY_DRIVER_H
  3. /*
  4. * This structure defines the interface between the low-level tty
  5. * driver and the tty routines. The following routines can be
  6. * defined; unless noted otherwise, they are optional, and can be
  7. * filled in with a null pointer.
  8. *
  9. * struct tty_struct * (*lookup)(struct tty_driver *self, int idx)
  10. *
  11. * Return the tty device corresponding to idx, NULL if there is not
  12. * one currently in use and an ERR_PTR value on error. Called under
  13. * tty_mutex (for now!)
  14. *
  15. * Optional method. Default behaviour is to use the ttys array
  16. *
  17. * int (*install)(struct tty_driver *self, struct tty_struct *tty)
  18. *
  19. * Install a new tty into the tty driver internal tables. Used in
  20. * conjunction with lookup and remove methods.
  21. *
  22. * Optional method. Default behaviour is to use the ttys array
  23. *
  24. * void (*remove)(struct tty_driver *self, struct tty_struct *tty)
  25. *
  26. * Remove a closed tty from the tty driver internal tables. Used in
  27. * conjunction with lookup and remove methods.
  28. *
  29. * Optional method. Default behaviour is to use the ttys array
  30. *
  31. * int (*open)(struct tty_struct * tty, struct file * filp);
  32. *
  33. * This routine is called when a particular tty device is opened.
  34. * This routine is mandatory; if this routine is not filled in,
  35. * the attempted open will fail with ENODEV.
  36. *
  37. * Required method.
  38. *
  39. * void (*close)(struct tty_struct * tty, struct file * filp);
  40. *
  41. * This routine is called when a particular tty device is closed.
  42. *
  43. * Required method.
  44. *
  45. * void (*shutdown)(struct tty_struct * tty);
  46. *
  47. * This routine is called synchronously when a particular tty device
  48. * is closed for the last time freeing up the resources.
  49. * Note that tty_shutdown() is not called if ops->shutdown is defined.
  50. * This means one is responsible to take care of calling ops->remove (e.g.
  51. * via tty_driver_remove_tty) and releasing tty->termios.
  52. * Note that this hook may be called from *all* the contexts where one
  53. * uses tty refcounting (e.g. tty_port_tty_get).
  54. *
  55. *
  56. * void (*cleanup)(struct tty_struct * tty);
  57. *
  58. * This routine is called asynchronously when a particular tty device
  59. * is closed for the last time freeing up the resources. This is
  60. * actually the second part of shutdown for routines that might sleep.
  61. *
  62. *
  63. * int (*write)(struct tty_struct * tty,
  64. * const unsigned char *buf, int count);
  65. *
  66. * This routine is called by the kernel to write a series of
  67. * characters to the tty device. The characters may come from
  68. * user space or kernel space. This routine will return the
  69. * number of characters actually accepted for writing.
  70. *
  71. * Optional: Required for writable devices.
  72. *
  73. * int (*put_char)(struct tty_struct *tty, unsigned char ch);
  74. *
  75. * This routine is called by the kernel to write a single
  76. * character to the tty device. If the kernel uses this routine,
  77. * it must call the flush_chars() routine (if defined) when it is
  78. * done stuffing characters into the driver. If there is no room
  79. * in the queue, the character is ignored.
  80. *
  81. * Optional: Kernel will use the write method if not provided.
  82. *
  83. * Note: Do not call this function directly, call tty_put_char
  84. *
  85. * void (*flush_chars)(struct tty_struct *tty);
  86. *
  87. * This routine is called by the kernel after it has written a
  88. * series of characters to the tty device using put_char().
  89. *
  90. * Optional:
  91. *
  92. * Note: Do not call this function directly, call tty_driver_flush_chars
  93. *
  94. * int (*write_room)(struct tty_struct *tty);
  95. *
  96. * This routine returns the numbers of characters the tty driver
  97. * will accept for queuing to be written. This number is subject
  98. * to change as output buffers get emptied, or if the output flow
  99. * control is acted.
  100. *
  101. * Required if write method is provided else not needed.
  102. *
  103. * Note: Do not call this function directly, call tty_write_room
  104. *
  105. * int (*ioctl)(struct tty_struct *tty, unsigned int cmd, unsigned long arg);
  106. *
  107. * This routine allows the tty driver to implement
  108. * device-specific ioctls. If the ioctl number passed in cmd
  109. * is not recognized by the driver, it should return ENOIOCTLCMD.
  110. *
  111. * Optional
  112. *
  113. * long (*compat_ioctl)(struct tty_struct *tty,,
  114. * unsigned int cmd, unsigned long arg);
  115. *
  116. * implement ioctl processing for 32 bit process on 64 bit system
  117. *
  118. * Optional
  119. *
  120. * void (*set_termios)(struct tty_struct *tty, struct ktermios * old);
  121. *
  122. * This routine allows the tty driver to be notified when
  123. * device's termios settings have changed.
  124. *
  125. * Optional: Called under the termios lock
  126. *
  127. *
  128. * void (*set_ldisc)(struct tty_struct *tty);
  129. *
  130. * This routine allows the tty driver to be notified when the
  131. * device's termios settings have changed.
  132. *
  133. * Optional: Called under BKL (currently)
  134. *
  135. * void (*throttle)(struct tty_struct * tty);
  136. *
  137. * This routine notifies the tty driver that input buffers for
  138. * the line discipline are close to full, and it should somehow
  139. * signal that no more characters should be sent to the tty.
  140. *
  141. * Optional: Always invoke via tty_throttle(), called under the
  142. * termios lock.
  143. *
  144. * void (*unthrottle)(struct tty_struct * tty);
  145. *
  146. * This routine notifies the tty drivers that it should signals
  147. * that characters can now be sent to the tty without fear of
  148. * overrunning the input buffers of the line disciplines.
  149. *
  150. * Optional: Always invoke via tty_unthrottle(), called under the
  151. * termios lock.
  152. *
  153. * void (*stop)(struct tty_struct *tty);
  154. *
  155. * This routine notifies the tty driver that it should stop
  156. * outputting characters to the tty device.
  157. *
  158. * Optional:
  159. *
  160. * Note: Call stop_tty not this method.
  161. *
  162. * void (*start)(struct tty_struct *tty);
  163. *
  164. * This routine notifies the tty driver that it resume sending
  165. * characters to the tty device.
  166. *
  167. * Optional:
  168. *
  169. * Note: Call start_tty not this method.
  170. *
  171. * void (*hangup)(struct tty_struct *tty);
  172. *
  173. * This routine notifies the tty driver that it should hang up the
  174. * tty device.
  175. *
  176. * Optional:
  177. *
  178. * int (*break_ctl)(struct tty_struct *tty, int state);
  179. *
  180. * This optional routine requests the tty driver to turn on or
  181. * off BREAK status on the RS-232 port. If state is -1,
  182. * then the BREAK status should be turned on; if state is 0, then
  183. * BREAK should be turned off.
  184. *
  185. * If this routine is implemented, the high-level tty driver will
  186. * handle the following ioctls: TCSBRK, TCSBRKP, TIOCSBRK,
  187. * TIOCCBRK.
  188. *
  189. * If the driver sets TTY_DRIVER_HARDWARE_BREAK then the interface
  190. * will also be called with actual times and the hardware is expected
  191. * to do the delay work itself. 0 and -1 are still used for on/off.
  192. *
  193. * Optional: Required for TCSBRK/BRKP/etc handling.
  194. *
  195. * void (*wait_until_sent)(struct tty_struct *tty, int timeout);
  196. *
  197. * This routine waits until the device has written out all of the
  198. * characters in its transmitter FIFO.
  199. *
  200. * Optional: If not provided the device is assumed to have no FIFO
  201. *
  202. * Note: Usually correct to call tty_wait_until_sent
  203. *
  204. * void (*send_xchar)(struct tty_struct *tty, char ch);
  205. *
  206. * This routine is used to send a high-priority XON/XOFF
  207. * character to the device.
  208. *
  209. * Optional: If not provided then the write method is called under
  210. * the atomic write lock to keep it serialized with the ldisc.
  211. *
  212. * int (*resize)(struct tty_struct *tty, struct winsize *ws)
  213. *
  214. * Called when a termios request is issued which changes the
  215. * requested terminal geometry.
  216. *
  217. * Optional: the default action is to update the termios structure
  218. * without error. This is usually the correct behaviour. Drivers should
  219. * not force errors here if they are not resizable objects (eg a serial
  220. * line). See tty_do_resize() if you need to wrap the standard method
  221. * in your own logic - the usual case.
  222. *
  223. * void (*set_termiox)(struct tty_struct *tty, struct termiox *new);
  224. *
  225. * Called when the device receives a termiox based ioctl. Passes down
  226. * the requested data from user space. This method will not be invoked
  227. * unless the tty also has a valid tty->termiox pointer.
  228. *
  229. * Optional: Called under the termios lock
  230. *
  231. * int (*get_icount)(struct tty_struct *tty, struct serial_icounter *icount);
  232. *
  233. * Called when the device receives a TIOCGICOUNT ioctl. Passed a kernel
  234. * structure to complete. This method is optional and will only be called
  235. * if provided (otherwise EINVAL will be returned).
  236. */
  237. #include <linux/export.h>
  238. #include <linux/fs.h>
  239. #include <linux/list.h>
  240. #include <linux/cdev.h>
  241. #include <linux/termios.h>
  242. struct tty_struct;
  243. struct tty_driver;
  244. struct serial_icounter_struct;
  245. struct tty_operations {
  246. struct tty_struct * (*lookup)(struct tty_driver *driver,
  247. struct inode *inode, int idx);
  248. int (*install)(struct tty_driver *driver, struct tty_struct *tty);
  249. void (*remove)(struct tty_driver *driver, struct tty_struct *tty);
  250. int (*open)(struct tty_struct * tty, struct file * filp);
  251. void (*close)(struct tty_struct * tty, struct file * filp);
  252. void (*shutdown)(struct tty_struct *tty);
  253. void (*cleanup)(struct tty_struct *tty);
  254. int (*write)(struct tty_struct * tty,
  255. const unsigned char *buf, int count);
  256. int (*put_char)(struct tty_struct *tty, unsigned char ch);
  257. void (*flush_chars)(struct tty_struct *tty);
  258. int (*write_room)(struct tty_struct *tty);
  259. int (*chars_in_buffer)(struct tty_struct *tty);
  260. int (*ioctl)(struct tty_struct *tty,
  261. unsigned int cmd, unsigned long arg);
  262. long (*compat_ioctl)(struct tty_struct *tty,
  263. unsigned int cmd, unsigned long arg);
  264. void (*set_termios)(struct tty_struct *tty, struct ktermios * old);
  265. void (*throttle)(struct tty_struct * tty);
  266. void (*unthrottle)(struct tty_struct * tty);
  267. void (*stop)(struct tty_struct *tty);
  268. void (*start)(struct tty_struct *tty);
  269. void (*hangup)(struct tty_struct *tty);
  270. int (*break_ctl)(struct tty_struct *tty, int state);
  271. void (*flush_buffer)(struct tty_struct *tty);
  272. void (*set_ldisc)(struct tty_struct *tty);
  273. void (*wait_until_sent)(struct tty_struct *tty, int timeout);
  274. void (*send_xchar)(struct tty_struct *tty, char ch);
  275. int (*tiocmget)(struct tty_struct *tty);
  276. int (*tiocmset)(struct tty_struct *tty,
  277. unsigned int set, unsigned int clear);
  278. int (*resize)(struct tty_struct *tty, struct winsize *ws);
  279. int (*set_termiox)(struct tty_struct *tty, struct termiox *tnew);
  280. int (*get_icount)(struct tty_struct *tty,
  281. struct serial_icounter_struct *icount);
  282. #ifdef CONFIG_CONSOLE_POLL
  283. int (*poll_init)(struct tty_driver *driver, int line, char *options);
  284. int (*poll_get_char)(struct tty_driver *driver, int line);
  285. void (*poll_put_char)(struct tty_driver *driver, int line, char ch);
  286. #endif
  287. const struct file_operations *proc_fops;
  288. };
  289. struct tty_driver {
  290. int magic; /* magic number for this structure */
  291. struct kref kref; /* Reference management */
  292. struct cdev cdev;
  293. struct module *owner;
  294. const char *driver_name;
  295. const char *name;
  296. int name_base; /* offset of printed name */
  297. int major; /* major device number */
  298. int minor_start; /* start of minor device number */
  299. int num; /* number of devices allocated */
  300. short type; /* type of tty driver */
  301. short subtype; /* subtype of tty driver */
  302. struct ktermios init_termios; /* Initial termios */
  303. int flags; /* tty driver flags */
  304. struct proc_dir_entry *proc_entry; /* /proc fs entry */
  305. struct tty_driver *other; /* only used for the PTY driver */
  306. /*
  307. * Pointer to the tty data structures
  308. */
  309. struct tty_struct **ttys;
  310. struct ktermios **termios;
  311. void *driver_state;
  312. /*
  313. * Driver methods
  314. */
  315. const struct tty_operations *ops;
  316. struct list_head tty_drivers;
  317. };
  318. extern struct list_head tty_drivers;
  319. extern struct tty_driver *__alloc_tty_driver(int lines, struct module *owner);
  320. extern void put_tty_driver(struct tty_driver *driver);
  321. extern void tty_set_operations(struct tty_driver *driver,
  322. const struct tty_operations *op);
  323. extern struct tty_driver *tty_find_polling_driver(char *name, int *line);
  324. extern void tty_driver_kref_put(struct tty_driver *driver);
  325. #define alloc_tty_driver(lines) __alloc_tty_driver(lines, THIS_MODULE)
  326. static inline struct tty_driver *tty_driver_kref_get(struct tty_driver *d)
  327. {
  328. kref_get(&d->kref);
  329. return d;
  330. }
  331. /* tty driver magic number */
  332. #define TTY_DRIVER_MAGIC 0x5402
  333. /*
  334. * tty driver flags
  335. *
  336. * TTY_DRIVER_RESET_TERMIOS --- requests the tty layer to reset the
  337. * termios setting when the last process has closed the device.
  338. * Used for PTY's, in particular.
  339. *
  340. * TTY_DRIVER_REAL_RAW --- if set, indicates that the driver will
  341. * guarantee never not to set any special character handling
  342. * flags if ((IGNBRK || (!BRKINT && !PARMRK)) && (IGNPAR ||
  343. * !INPCK)). That is, if there is no reason for the driver to
  344. * send notifications of parity and break characters up to the
  345. * line driver, it won't do so. This allows the line driver to
  346. * optimize for this case if this flag is set. (Note that there
  347. * is also a promise, if the above case is true, not to signal
  348. * overruns, either.)
  349. *
  350. * TTY_DRIVER_DYNAMIC_DEV --- if set, the individual tty devices need
  351. * to be registered with a call to tty_register_device() when the
  352. * device is found in the system and unregistered with a call to
  353. * tty_unregister_device() so the devices will be show up
  354. * properly in sysfs. If not set, driver->num entries will be
  355. * created by the tty core in sysfs when tty_register_driver() is
  356. * called. This is to be used by drivers that have tty devices
  357. * that can appear and disappear while the main tty driver is
  358. * registered with the tty core.
  359. *
  360. * TTY_DRIVER_DEVPTS_MEM -- don't use the standard arrays, instead
  361. * use dynamic memory keyed through the devpts filesystem. This
  362. * is only applicable to the pty driver.
  363. *
  364. * TTY_DRIVER_HARDWARE_BREAK -- hardware handles break signals. Pass
  365. * the requested timeout to the caller instead of using a simple
  366. * on/off interface.
  367. *
  368. */
  369. #define TTY_DRIVER_INSTALLED 0x0001
  370. #define TTY_DRIVER_RESET_TERMIOS 0x0002
  371. #define TTY_DRIVER_REAL_RAW 0x0004
  372. #define TTY_DRIVER_DYNAMIC_DEV 0x0008
  373. #define TTY_DRIVER_DEVPTS_MEM 0x0010
  374. #define TTY_DRIVER_HARDWARE_BREAK 0x0020
  375. /* tty driver types */
  376. #define TTY_DRIVER_TYPE_SYSTEM 0x0001
  377. #define TTY_DRIVER_TYPE_CONSOLE 0x0002
  378. #define TTY_DRIVER_TYPE_SERIAL 0x0003
  379. #define TTY_DRIVER_TYPE_PTY 0x0004
  380. #define TTY_DRIVER_TYPE_SCC 0x0005 /* scc driver */
  381. #define TTY_DRIVER_TYPE_SYSCONS 0x0006
  382. /* system subtypes (magic, used by tty_io.c) */
  383. #define SYSTEM_TYPE_TTY 0x0001
  384. #define SYSTEM_TYPE_CONSOLE 0x0002
  385. #define SYSTEM_TYPE_SYSCONS 0x0003
  386. #define SYSTEM_TYPE_SYSPTMX 0x0004
  387. /* pty subtypes (magic, used by tty_io.c) */
  388. #define PTY_TYPE_MASTER 0x0001
  389. #define PTY_TYPE_SLAVE 0x0002
  390. /* serial subtype definitions */
  391. #define SERIAL_TYPE_NORMAL 1
  392. #endif /* #ifdef _LINUX_TTY_DRIVER_H */