tty_driver.h 14 KB

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