tty_driver.h 13 KB

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