tty_driver.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 under the tty lock when a particular tty device
  48. * is closed for the last time. It executes before the tty resources
  49. * are freed so may execute while another function holds a tty kref.
  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, unsigned int cmd, unsigned long arg);
  101. *
  102. * This routine allows the tty driver to implement
  103. * device-specific ioctls. If the ioctl number passed in cmd
  104. * is not recognized by the driver, it should return ENOIOCTLCMD.
  105. *
  106. * Optional
  107. *
  108. * long (*compat_ioctl)(struct tty_struct *tty,,
  109. * unsigned int cmd, unsigned long arg);
  110. *
  111. * implement ioctl processing for 32 bit process on 64 bit system
  112. *
  113. * Optional
  114. *
  115. * void (*set_termios)(struct tty_struct *tty, struct ktermios * old);
  116. *
  117. * This routine allows the tty driver to be notified when
  118. * device's termios settings have changed.
  119. *
  120. * Optional: Called under the termios lock
  121. *
  122. *
  123. * void (*set_ldisc)(struct tty_struct *tty);
  124. *
  125. * This routine allows the tty driver to be notified when the
  126. * device's termios settings have changed.
  127. *
  128. * Optional: Called under BKL (currently)
  129. *
  130. * void (*throttle)(struct tty_struct * tty);
  131. *
  132. * This routine notifies the tty driver that input buffers for
  133. * the line discipline are close to full, and it should somehow
  134. * signal that no more characters should be sent to the tty.
  135. *
  136. * Optional: Always invoke via tty_throttle(), called under the
  137. * termios lock.
  138. *
  139. * void (*unthrottle)(struct tty_struct * tty);
  140. *
  141. * This routine notifies the tty drivers that it should signals
  142. * that characters can now be sent to the tty without fear of
  143. * overrunning the input buffers of the line disciplines.
  144. *
  145. * Optional: Always invoke via tty_unthrottle(), called under the
  146. * termios lock.
  147. *
  148. * void (*stop)(struct tty_struct *tty);
  149. *
  150. * This routine notifies the tty driver that it should stop
  151. * outputting characters to the tty device.
  152. *
  153. * Optional:
  154. *
  155. * Note: Call stop_tty not this method.
  156. *
  157. * void (*start)(struct tty_struct *tty);
  158. *
  159. * This routine notifies the tty driver that it resume sending
  160. * characters to the tty device.
  161. *
  162. * Optional:
  163. *
  164. * Note: Call start_tty not this method.
  165. *
  166. * void (*hangup)(struct tty_struct *tty);
  167. *
  168. * This routine notifies the tty driver that it should hang up the
  169. * tty device.
  170. *
  171. * Optional:
  172. *
  173. * int (*break_ctl)(struct tty_struct *tty, int state);
  174. *
  175. * This optional routine requests the tty driver to turn on or
  176. * off BREAK status on the RS-232 port. If state is -1,
  177. * then the BREAK status should be turned on; if state is 0, then
  178. * BREAK should be turned off.
  179. *
  180. * If this routine is implemented, the high-level tty driver will
  181. * handle the following ioctls: TCSBRK, TCSBRKP, TIOCSBRK,
  182. * TIOCCBRK.
  183. *
  184. * If the driver sets TTY_DRIVER_HARDWARE_BREAK then the interface
  185. * will also be called with actual times and the hardware is expected
  186. * to do the delay work itself. 0 and -1 are still used for on/off.
  187. *
  188. * Optional: Required for TCSBRK/BRKP/etc handling.
  189. *
  190. * void (*wait_until_sent)(struct tty_struct *tty, int timeout);
  191. *
  192. * This routine waits until the device has written out all of the
  193. * characters in its transmitter FIFO.
  194. *
  195. * Optional: If not provided the device is assumed to have no FIFO
  196. *
  197. * Note: Usually correct to call tty_wait_until_sent
  198. *
  199. * void (*send_xchar)(struct tty_struct *tty, char ch);
  200. *
  201. * This routine is used to send a high-priority XON/XOFF
  202. * character to the device.
  203. *
  204. * Optional: If not provided then the write method is called under
  205. * the atomic write lock to keep it serialized with the ldisc.
  206. *
  207. * int (*resize)(struct tty_struct *tty, struct winsize *ws)
  208. *
  209. * Called when a termios request is issued which changes the
  210. * requested terminal geometry.
  211. *
  212. * Optional: the default action is to update the termios structure
  213. * without error. This is usually the correct behaviour. Drivers should
  214. * not force errors here if they are not resizable objects (eg a serial
  215. * line). See tty_do_resize() if you need to wrap the standard method
  216. * in your own logic - the usual case.
  217. *
  218. * void (*set_termiox)(struct tty_struct *tty, struct termiox *new);
  219. *
  220. * Called when the device receives a termiox based ioctl. Passes down
  221. * the requested data from user space. This method will not be invoked
  222. * unless the tty also has a valid tty->termiox pointer.
  223. *
  224. * Optional: Called under the termios lock
  225. *
  226. * int (*get_icount)(struct tty_struct *tty, struct serial_icounter *icount);
  227. *
  228. * Called when the device receives a TIOCGICOUNT ioctl. Passed a kernel
  229. * structure to complete. This method is optional and will only be called
  230. * if provided (otherwise EINVAL will be returned).
  231. */
  232. #include <linux/export.h>
  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,
  256. unsigned int cmd, unsigned long arg);
  257. long (*compat_ioctl)(struct tty_struct *tty,
  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);
  271. int (*tiocmset)(struct tty_struct *tty,
  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. unsigned int num; /* number of devices allocated */
  295. short type; /* type of tty driver */
  296. short subtype; /* subtype of tty driver */
  297. struct ktermios init_termios; /* Initial termios */
  298. unsigned long flags; /* tty driver flags */
  299. struct proc_dir_entry *proc_entry; /* /proc fs entry */
  300. struct tty_driver *other; /* only used for the PTY driver */
  301. /*
  302. * Pointer to the tty data structures
  303. */
  304. struct tty_struct **ttys;
  305. struct tty_port **ports;
  306. struct ktermios **termios;
  307. void *driver_state;
  308. /*
  309. * Driver methods
  310. */
  311. const struct tty_operations *ops;
  312. struct list_head tty_drivers;
  313. };
  314. extern struct list_head tty_drivers;
  315. extern struct tty_driver *__tty_alloc_driver(unsigned int lines,
  316. struct module *owner, unsigned long flags);
  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. /* Use TTY_DRIVER_* flags below */
  323. #define tty_alloc_driver(lines, flags) \
  324. __tty_alloc_driver(lines, THIS_MODULE, flags)
  325. /*
  326. * DEPRECATED Do not use this in new code, use tty_alloc_driver instead.
  327. * (And change the return value checks.)
  328. */
  329. static inline struct tty_driver *alloc_tty_driver(unsigned int lines)
  330. {
  331. struct tty_driver *ret = tty_alloc_driver(lines, 0);
  332. if (IS_ERR(ret))
  333. return NULL;
  334. return ret;
  335. }
  336. static inline struct tty_driver *tty_driver_kref_get(struct tty_driver *d)
  337. {
  338. kref_get(&d->kref);
  339. return d;
  340. }
  341. /* tty driver magic number */
  342. #define TTY_DRIVER_MAGIC 0x5402
  343. /*
  344. * tty driver flags
  345. *
  346. * TTY_DRIVER_RESET_TERMIOS --- requests the tty layer to reset the
  347. * termios setting when the last process has closed the device.
  348. * Used for PTY's, in particular.
  349. *
  350. * TTY_DRIVER_REAL_RAW --- if set, indicates that the driver will
  351. * guarantee never not to set any special character handling
  352. * flags if ((IGNBRK || (!BRKINT && !PARMRK)) && (IGNPAR ||
  353. * !INPCK)). That is, if there is no reason for the driver to
  354. * send notifications of parity and break characters up to the
  355. * line driver, it won't do so. This allows the line driver to
  356. * optimize for this case if this flag is set. (Note that there
  357. * is also a promise, if the above case is true, not to signal
  358. * overruns, either.)
  359. *
  360. * TTY_DRIVER_DYNAMIC_DEV --- if set, the individual tty devices need
  361. * to be registered with a call to tty_register_device() when the
  362. * device is found in the system and unregistered with a call to
  363. * tty_unregister_device() so the devices will be show up
  364. * properly in sysfs. If not set, driver->num entries will be
  365. * created by the tty core in sysfs when tty_register_driver() is
  366. * called. This is to be used by drivers that have tty devices
  367. * that can appear and disappear while the main tty driver is
  368. * registered with the tty core.
  369. *
  370. * TTY_DRIVER_DEVPTS_MEM -- don't use the standard arrays, instead
  371. * use dynamic memory keyed through the devpts filesystem. This
  372. * is only applicable to the pty driver.
  373. *
  374. * TTY_DRIVER_HARDWARE_BREAK -- hardware handles break signals. Pass
  375. * the requested timeout to the caller instead of using a simple
  376. * on/off interface.
  377. *
  378. */
  379. #define TTY_DRIVER_INSTALLED 0x0001
  380. #define TTY_DRIVER_RESET_TERMIOS 0x0002
  381. #define TTY_DRIVER_REAL_RAW 0x0004
  382. #define TTY_DRIVER_DYNAMIC_DEV 0x0008
  383. #define TTY_DRIVER_DEVPTS_MEM 0x0010
  384. #define TTY_DRIVER_HARDWARE_BREAK 0x0020
  385. /* tty driver types */
  386. #define TTY_DRIVER_TYPE_SYSTEM 0x0001
  387. #define TTY_DRIVER_TYPE_CONSOLE 0x0002
  388. #define TTY_DRIVER_TYPE_SERIAL 0x0003
  389. #define TTY_DRIVER_TYPE_PTY 0x0004
  390. #define TTY_DRIVER_TYPE_SCC 0x0005 /* scc driver */
  391. #define TTY_DRIVER_TYPE_SYSCONS 0x0006
  392. /* system subtypes (magic, used by tty_io.c) */
  393. #define SYSTEM_TYPE_TTY 0x0001
  394. #define SYSTEM_TYPE_CONSOLE 0x0002
  395. #define SYSTEM_TYPE_SYSCONS 0x0003
  396. #define SYSTEM_TYPE_SYSPTMX 0x0004
  397. /* pty subtypes (magic, used by tty_io.c) */
  398. #define PTY_TYPE_MASTER 0x0001
  399. #define PTY_TYPE_SLAVE 0x0002
  400. /* serial subtype definitions */
  401. #define SERIAL_TYPE_NORMAL 1
  402. #endif /* #ifdef _LINUX_TTY_DRIVER_H */