tty_driver.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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, struct file *, 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. Called with tty lock held.
  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. * Note: called even if the corresponding open() failed.
  43. *
  44. * Required method. Called with tty lock held.
  45. *
  46. * void (*shutdown)(struct tty_struct * tty);
  47. *
  48. * This routine is called under the tty lock when a particular tty device
  49. * is closed for the last time. It executes before the tty resources
  50. * are freed so may execute while another function holds a tty kref.
  51. *
  52. * void (*cleanup)(struct tty_struct * tty);
  53. *
  54. * This routine is called asynchronously when a particular tty device
  55. * is closed for the last time freeing up the resources. This is
  56. * actually the second part of shutdown for routines that might sleep.
  57. *
  58. *
  59. * int (*write)(struct tty_struct * tty,
  60. * const unsigned char *buf, int count);
  61. *
  62. * This routine is called by the kernel to write a series of
  63. * characters to the tty device. The characters may come from
  64. * user space or kernel space. This routine will return the
  65. * number of characters actually accepted for writing.
  66. *
  67. * Optional: Required for writable devices.
  68. *
  69. * int (*put_char)(struct tty_struct *tty, unsigned char ch);
  70. *
  71. * This routine is called by the kernel to write a single
  72. * character to the tty device. If the kernel uses this routine,
  73. * it must call the flush_chars() routine (if defined) when it is
  74. * done stuffing characters into the driver. If there is no room
  75. * in the queue, the character is ignored.
  76. *
  77. * Optional: Kernel will use the write method if not provided.
  78. *
  79. * Note: Do not call this function directly, call tty_put_char
  80. *
  81. * void (*flush_chars)(struct tty_struct *tty);
  82. *
  83. * This routine is called by the kernel after it has written a
  84. * series of characters to the tty device using put_char().
  85. *
  86. * Optional:
  87. *
  88. * Note: Do not call this function directly, call tty_driver_flush_chars
  89. *
  90. * int (*write_room)(struct tty_struct *tty);
  91. *
  92. * This routine returns the numbers of characters the tty driver
  93. * will accept for queuing to be written. This number is subject
  94. * to change as output buffers get emptied, or if the output flow
  95. * control is acted.
  96. *
  97. * Required if write method is provided else not needed.
  98. *
  99. * Note: Do not call this function directly, call tty_write_room
  100. *
  101. * int (*ioctl)(struct tty_struct *tty, 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,,
  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. * Called with ->flow_lock held. Serialized with start() method.
  155. *
  156. * Optional:
  157. *
  158. * Note: Call stop_tty not this method.
  159. *
  160. * void (*start)(struct tty_struct *tty);
  161. *
  162. * This routine notifies the tty driver that it resume sending
  163. * characters to the tty device.
  164. *
  165. * Called with ->flow_lock held. Serialized with stop() method.
  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. * Called with tty lock held.
  179. *
  180. * int (*break_ctl)(struct tty_struct *tty, int state);
  181. *
  182. * This optional routine requests the tty driver to turn on or
  183. * off BREAK status on the RS-232 port. If state is -1,
  184. * then the BREAK status should be turned on; if state is 0, then
  185. * BREAK should be turned off.
  186. *
  187. * If this routine is implemented, the high-level tty driver will
  188. * handle the following ioctls: TCSBRK, TCSBRKP, TIOCSBRK,
  189. * TIOCCBRK.
  190. *
  191. * If the driver sets TTY_DRIVER_HARDWARE_BREAK then the interface
  192. * will also be called with actual times and the hardware is expected
  193. * to do the delay work itself. 0 and -1 are still used for on/off.
  194. *
  195. * Optional: Required for TCSBRK/BRKP/etc handling.
  196. *
  197. * void (*wait_until_sent)(struct tty_struct *tty, int timeout);
  198. *
  199. * This routine waits until the device has written out all of the
  200. * characters in its transmitter FIFO.
  201. *
  202. * Optional: If not provided the device is assumed to have no FIFO
  203. *
  204. * Note: Usually correct to call tty_wait_until_sent
  205. *
  206. * void (*send_xchar)(struct tty_struct *tty, char ch);
  207. *
  208. * This routine is used to send a high-priority XON/XOFF
  209. * character to the device.
  210. *
  211. * Optional: If not provided then the write method is called under
  212. * the atomic write lock to keep it serialized with the ldisc.
  213. *
  214. * int (*resize)(struct tty_struct *tty, struct winsize *ws)
  215. *
  216. * Called when a termios request is issued which changes the
  217. * requested terminal geometry.
  218. *
  219. * Optional: the default action is to update the termios structure
  220. * without error. This is usually the correct behaviour. Drivers should
  221. * not force errors here if they are not resizable objects (eg a serial
  222. * line). See tty_do_resize() if you need to wrap the standard method
  223. * in your own logic - the usual case.
  224. *
  225. * void (*set_termiox)(struct tty_struct *tty, struct termiox *new);
  226. *
  227. * Called when the device receives a termiox based ioctl. Passes down
  228. * the requested data from user space. This method will not be invoked
  229. * unless the tty also has a valid tty->termiox pointer.
  230. *
  231. * Optional: Called under the termios lock
  232. *
  233. * int (*get_icount)(struct tty_struct *tty, struct serial_icounter *icount);
  234. *
  235. * Called when the device receives a TIOCGICOUNT ioctl. Passed a kernel
  236. * structure to complete. This method is optional and will only be called
  237. * if provided (otherwise EINVAL will be returned).
  238. */
  239. #include <linux/export.h>
  240. #include <linux/fs.h>
  241. #include <linux/list.h>
  242. #include <linux/cdev.h>
  243. #include <linux/termios.h>
  244. struct tty_struct;
  245. struct tty_driver;
  246. struct serial_icounter_struct;
  247. struct tty_operations {
  248. struct tty_struct * (*lookup)(struct tty_driver *driver,
  249. struct file *filp, int idx);
  250. int (*install)(struct tty_driver *driver, struct tty_struct *tty);
  251. void (*remove)(struct tty_driver *driver, struct tty_struct *tty);
  252. int (*open)(struct tty_struct * tty, struct file * filp);
  253. void (*close)(struct tty_struct * tty, struct file * filp);
  254. void (*shutdown)(struct tty_struct *tty);
  255. void (*cleanup)(struct tty_struct *tty);
  256. int (*write)(struct tty_struct * tty,
  257. const unsigned char *buf, int count);
  258. int (*put_char)(struct tty_struct *tty, unsigned char ch);
  259. void (*flush_chars)(struct tty_struct *tty);
  260. int (*write_room)(struct tty_struct *tty);
  261. int (*chars_in_buffer)(struct tty_struct *tty);
  262. int (*ioctl)(struct tty_struct *tty,
  263. unsigned int cmd, unsigned long arg);
  264. long (*compat_ioctl)(struct tty_struct *tty,
  265. unsigned int cmd, unsigned long arg);
  266. void (*set_termios)(struct tty_struct *tty, struct ktermios * old);
  267. void (*throttle)(struct tty_struct * tty);
  268. void (*unthrottle)(struct tty_struct * tty);
  269. void (*stop)(struct tty_struct *tty);
  270. void (*start)(struct tty_struct *tty);
  271. void (*hangup)(struct tty_struct *tty);
  272. int (*break_ctl)(struct tty_struct *tty, int state);
  273. void (*flush_buffer)(struct tty_struct *tty);
  274. void (*set_ldisc)(struct tty_struct *tty);
  275. void (*wait_until_sent)(struct tty_struct *tty, int timeout);
  276. void (*send_xchar)(struct tty_struct *tty, char ch);
  277. int (*tiocmget)(struct tty_struct *tty);
  278. int (*tiocmset)(struct tty_struct *tty,
  279. unsigned int set, unsigned int clear);
  280. int (*resize)(struct tty_struct *tty, struct winsize *ws);
  281. int (*set_termiox)(struct tty_struct *tty, struct termiox *tnew);
  282. int (*get_icount)(struct tty_struct *tty,
  283. struct serial_icounter_struct *icount);
  284. #ifdef CONFIG_CONSOLE_POLL
  285. int (*poll_init)(struct tty_driver *driver, int line, char *options);
  286. int (*poll_get_char)(struct tty_driver *driver, int line);
  287. void (*poll_put_char)(struct tty_driver *driver, int line, char ch);
  288. #endif
  289. const struct file_operations *proc_fops;
  290. };
  291. struct tty_driver {
  292. int magic; /* magic number for this structure */
  293. struct kref kref; /* Reference management */
  294. struct cdev **cdevs;
  295. struct module *owner;
  296. const char *driver_name;
  297. const char *name;
  298. int name_base; /* offset of printed name */
  299. int major; /* major device number */
  300. int minor_start; /* start of minor device number */
  301. unsigned int num; /* number of devices allocated */
  302. short type; /* type of tty driver */
  303. short subtype; /* subtype of tty driver */
  304. struct ktermios init_termios; /* Initial termios */
  305. unsigned long flags; /* tty driver flags */
  306. struct proc_dir_entry *proc_entry; /* /proc fs entry */
  307. struct tty_driver *other; /* only used for the PTY driver */
  308. /*
  309. * Pointer to the tty data structures
  310. */
  311. struct tty_struct **ttys;
  312. struct tty_port **ports;
  313. struct ktermios **termios;
  314. void *driver_state;
  315. /*
  316. * Driver methods
  317. */
  318. const struct tty_operations *ops;
  319. struct list_head tty_drivers;
  320. };
  321. extern struct list_head tty_drivers;
  322. extern struct tty_driver *__tty_alloc_driver(unsigned int lines,
  323. struct module *owner, unsigned long flags);
  324. extern void put_tty_driver(struct tty_driver *driver);
  325. extern void tty_set_operations(struct tty_driver *driver,
  326. const struct tty_operations *op);
  327. extern struct tty_driver *tty_find_polling_driver(char *name, int *line);
  328. extern void tty_driver_kref_put(struct tty_driver *driver);
  329. /* Use TTY_DRIVER_* flags below */
  330. #define tty_alloc_driver(lines, flags) \
  331. __tty_alloc_driver(lines, THIS_MODULE, flags)
  332. /*
  333. * DEPRECATED Do not use this in new code, use tty_alloc_driver instead.
  334. * (And change the return value checks.)
  335. */
  336. static inline struct tty_driver *alloc_tty_driver(unsigned int lines)
  337. {
  338. struct tty_driver *ret = tty_alloc_driver(lines, 0);
  339. if (IS_ERR(ret))
  340. return NULL;
  341. return ret;
  342. }
  343. static inline struct tty_driver *tty_driver_kref_get(struct tty_driver *d)
  344. {
  345. kref_get(&d->kref);
  346. return d;
  347. }
  348. /* tty driver magic number */
  349. #define TTY_DRIVER_MAGIC 0x5402
  350. /*
  351. * tty driver flags
  352. *
  353. * TTY_DRIVER_RESET_TERMIOS --- requests the tty layer to reset the
  354. * termios setting when the last process has closed the device.
  355. * Used for PTY's, in particular.
  356. *
  357. * TTY_DRIVER_REAL_RAW --- if set, indicates that the driver will
  358. * guarantee never not to set any special character handling
  359. * flags if ((IGNBRK || (!BRKINT && !PARMRK)) && (IGNPAR ||
  360. * !INPCK)). That is, if there is no reason for the driver to
  361. * send notifications of parity and break characters up to the
  362. * line driver, it won't do so. This allows the line driver to
  363. * optimize for this case if this flag is set. (Note that there
  364. * is also a promise, if the above case is true, not to signal
  365. * overruns, either.)
  366. *
  367. * TTY_DRIVER_DYNAMIC_DEV --- if set, the individual tty devices need
  368. * to be registered with a call to tty_register_device() when the
  369. * device is found in the system and unregistered with a call to
  370. * tty_unregister_device() so the devices will be show up
  371. * properly in sysfs. If not set, driver->num entries will be
  372. * created by the tty core in sysfs when tty_register_driver() is
  373. * called. This is to be used by drivers that have tty devices
  374. * that can appear and disappear while the main tty driver is
  375. * registered with the tty core.
  376. *
  377. * TTY_DRIVER_DEVPTS_MEM -- don't use the standard arrays, instead
  378. * use dynamic memory keyed through the devpts filesystem. This
  379. * is only applicable to the pty driver.
  380. *
  381. * TTY_DRIVER_HARDWARE_BREAK -- hardware handles break signals. Pass
  382. * the requested timeout to the caller instead of using a simple
  383. * on/off interface.
  384. *
  385. * TTY_DRIVER_DYNAMIC_ALLOC -- do not allocate structures which are
  386. * needed per line for this driver as it would waste memory.
  387. * The driver will take care.
  388. *
  389. * TTY_DRIVER_UNNUMBERED_NODE -- do not create numbered /dev nodes. In
  390. * other words create /dev/ttyprintk and not /dev/ttyprintk0.
  391. * Applicable only when a driver for a single tty device is
  392. * being allocated.
  393. */
  394. #define TTY_DRIVER_INSTALLED 0x0001
  395. #define TTY_DRIVER_RESET_TERMIOS 0x0002
  396. #define TTY_DRIVER_REAL_RAW 0x0004
  397. #define TTY_DRIVER_DYNAMIC_DEV 0x0008
  398. #define TTY_DRIVER_DEVPTS_MEM 0x0010
  399. #define TTY_DRIVER_HARDWARE_BREAK 0x0020
  400. #define TTY_DRIVER_DYNAMIC_ALLOC 0x0040
  401. #define TTY_DRIVER_UNNUMBERED_NODE 0x0080
  402. /* tty driver types */
  403. #define TTY_DRIVER_TYPE_SYSTEM 0x0001
  404. #define TTY_DRIVER_TYPE_CONSOLE 0x0002
  405. #define TTY_DRIVER_TYPE_SERIAL 0x0003
  406. #define TTY_DRIVER_TYPE_PTY 0x0004
  407. #define TTY_DRIVER_TYPE_SCC 0x0005 /* scc driver */
  408. #define TTY_DRIVER_TYPE_SYSCONS 0x0006
  409. /* system subtypes (magic, used by tty_io.c) */
  410. #define SYSTEM_TYPE_TTY 0x0001
  411. #define SYSTEM_TYPE_CONSOLE 0x0002
  412. #define SYSTEM_TYPE_SYSCONS 0x0003
  413. #define SYSTEM_TYPE_SYSPTMX 0x0004
  414. /* pty subtypes (magic, used by tty_io.c) */
  415. #define PTY_TYPE_MASTER 0x0001
  416. #define PTY_TYPE_SLAVE 0x0002
  417. /* serial subtype definitions */
  418. #define SERIAL_TYPE_NORMAL 1
  419. #endif /* #ifdef _LINUX_TTY_DRIVER_H */