xref: /openbmc/linux/include/linux/tty_driver.h (revision 95713967)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds #ifndef _LINUX_TTY_DRIVER_H
31da177e4SLinus Torvalds #define _LINUX_TTY_DRIVER_H
41da177e4SLinus Torvalds 
51a54a76dSJiri Slaby #include <linux/export.h>
61da177e4SLinus Torvalds #include <linux/fs.h>
7890ebae6SJiri Slaby #include <linux/kref.h>
81da177e4SLinus Torvalds #include <linux/list.h>
91da177e4SLinus Torvalds #include <linux/cdev.h>
1089bbeb7eSAl Viro #include <linux/uaccess.h>
113dfbd044SJiri Slaby #include <linux/termios.h>
12d01c3289SMasatake YAMATO #include <linux/seq_file.h>
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds struct tty_struct;
15f2d937f3SJason Wessel struct tty_driver;
16d281da7fSAlan Cox struct serial_icounter_struct;
172f46a2c1SAl Viro struct serial_struct;
181da177e4SLinus Torvalds 
191fe18309SJiri Slaby /**
201fe18309SJiri Slaby  * struct tty_operations -- interface between driver and tty
211fe18309SJiri Slaby  *
221fe18309SJiri Slaby  * @lookup: ``struct tty_struct *()(struct tty_driver *self, struct file *,
231fe18309SJiri Slaby  *				    int idx)``
241fe18309SJiri Slaby  *
251fe18309SJiri Slaby  *	Return the tty device corresponding to @idx, %NULL if there is not
261fe18309SJiri Slaby  *	one currently in use and an %ERR_PTR value on error. Called under
271fe18309SJiri Slaby  *	%tty_mutex (for now!)
281fe18309SJiri Slaby  *
291fe18309SJiri Slaby  *	Optional method. Default behaviour is to use the @self->ttys array.
301fe18309SJiri Slaby  *
311fe18309SJiri Slaby  * @install: ``int ()(struct tty_driver *self, struct tty_struct *tty)``
321fe18309SJiri Slaby  *
331fe18309SJiri Slaby  *	Install a new @tty into the @self's internal tables. Used in
341fe18309SJiri Slaby  *	conjunction with @lookup and @remove methods.
351fe18309SJiri Slaby  *
361fe18309SJiri Slaby  *	Optional method. Default behaviour is to use the @self->ttys array.
371fe18309SJiri Slaby  *
381fe18309SJiri Slaby  * @remove: ``void ()(struct tty_driver *self, struct tty_struct *tty)``
391fe18309SJiri Slaby  *
401fe18309SJiri Slaby  *	Remove a closed @tty from the @self's internal tables. Used in
411fe18309SJiri Slaby  *	conjunction with @lookup and @remove methods.
421fe18309SJiri Slaby  *
431fe18309SJiri Slaby  *	Optional method. Default behaviour is to use the @self->ttys array.
441fe18309SJiri Slaby  *
451fe18309SJiri Slaby  * @open: ``int ()(struct tty_struct *tty, struct file *)``
461fe18309SJiri Slaby  *
471fe18309SJiri Slaby  *	This routine is called when a particular @tty device is opened. This
481fe18309SJiri Slaby  *	routine is mandatory; if this routine is not filled in, the attempted
491fe18309SJiri Slaby  *	open will fail with %ENODEV.
501fe18309SJiri Slaby  *
5129d5ef68SJiri Slaby  *	Required method. Called with tty lock held. May sleep.
521fe18309SJiri Slaby  *
531fe18309SJiri Slaby  * @close: ``void ()(struct tty_struct *tty, struct file *)``
541fe18309SJiri Slaby  *
5529d5ef68SJiri Slaby  *	This routine is called when a particular @tty device is closed. At the
5629d5ef68SJiri Slaby  *	point of return from this call the driver must make no further ldisc
5729d5ef68SJiri Slaby  *	calls of any kind.
581fe18309SJiri Slaby  *
591fe18309SJiri Slaby  *	Remark: called even if the corresponding @open() failed.
601fe18309SJiri Slaby  *
6129d5ef68SJiri Slaby  *	Required method. Called with tty lock held. May sleep.
621fe18309SJiri Slaby  *
631fe18309SJiri Slaby  * @shutdown: ``void ()(struct tty_struct *tty)``
641fe18309SJiri Slaby  *
651fe18309SJiri Slaby  *	This routine is called under the tty lock when a particular @tty device
661fe18309SJiri Slaby  *	is closed for the last time. It executes before the @tty resources
671fe18309SJiri Slaby  *	are freed so may execute while another function holds a @tty kref.
681fe18309SJiri Slaby  *
691fe18309SJiri Slaby  * @cleanup: ``void ()(struct tty_struct *tty)``
701fe18309SJiri Slaby  *
711fe18309SJiri Slaby  *	This routine is called asynchronously when a particular @tty device
721fe18309SJiri Slaby  *	is closed for the last time freeing up the resources. This is
731fe18309SJiri Slaby  *	actually the second part of shutdown for routines that might sleep.
741fe18309SJiri Slaby  *
75*95713967SJiri Slaby (SUSE)  * @write: ``ssize_t ()(struct tty_struct *tty, const unsigned char *buf,
76*95713967SJiri Slaby (SUSE)  *		    size_t count)``
771fe18309SJiri Slaby  *
781fe18309SJiri Slaby  *	This routine is called by the kernel to write a series (@count) of
791fe18309SJiri Slaby  *	characters (@buf) to the @tty device. The characters may come from
801fe18309SJiri Slaby  *	user space or kernel space.  This routine will return the
811fe18309SJiri Slaby  *	number of characters actually accepted for writing.
821fe18309SJiri Slaby  *
8329d5ef68SJiri Slaby  *	May occur in parallel in special cases. Because this includes panic
8429d5ef68SJiri Slaby  *	paths drivers generally shouldn't try and do clever locking here.
8529d5ef68SJiri Slaby  *
8629d5ef68SJiri Slaby  *	Optional: Required for writable devices. May not sleep.
871fe18309SJiri Slaby  *
881fe18309SJiri Slaby  * @put_char: ``int ()(struct tty_struct *tty, unsigned char ch)``
891fe18309SJiri Slaby  *
901fe18309SJiri Slaby  *	This routine is called by the kernel to write a single character @ch to
911fe18309SJiri Slaby  *	the @tty device. If the kernel uses this routine, it must call the
921fe18309SJiri Slaby  *	@flush_chars() routine (if defined) when it is done stuffing characters
931fe18309SJiri Slaby  *	into the driver. If there is no room in the queue, the character is
941fe18309SJiri Slaby  *	ignored.
951fe18309SJiri Slaby  *
961fe18309SJiri Slaby  *	Optional: Kernel will use the @write method if not provided. Do not
971fe18309SJiri Slaby  *	call this function directly, call tty_put_char().
981fe18309SJiri Slaby  *
991fe18309SJiri Slaby  * @flush_chars: ``void ()(struct tty_struct *tty)``
1001fe18309SJiri Slaby  *
1011fe18309SJiri Slaby  *	This routine is called by the kernel after it has written a
1021fe18309SJiri Slaby  *	series of characters to the tty device using @put_char().
1031fe18309SJiri Slaby  *
1041fe18309SJiri Slaby  *	Optional. Do not call this function directly, call
1051fe18309SJiri Slaby  *	tty_driver_flush_chars().
1061fe18309SJiri Slaby  *
1071fe18309SJiri Slaby  * @write_room: ``unsigned int ()(struct tty_struct *tty)``
1081fe18309SJiri Slaby  *
1091fe18309SJiri Slaby  *	This routine returns the numbers of characters the @tty driver
1101fe18309SJiri Slaby  *	will accept for queuing to be written.  This number is subject
1111fe18309SJiri Slaby  *	to change as output buffers get emptied, or if the output flow
1121fe18309SJiri Slaby  *	control is acted.
1131fe18309SJiri Slaby  *
11429d5ef68SJiri Slaby  *	The ldisc is responsible for being intelligent about multi-threading of
11529d5ef68SJiri Slaby  *	write_room/write calls
11629d5ef68SJiri Slaby  *
1171fe18309SJiri Slaby  *	Required if @write method is provided else not needed. Do not call this
1181fe18309SJiri Slaby  *	function directly, call tty_write_room()
1191fe18309SJiri Slaby  *
1201fe18309SJiri Slaby  * @chars_in_buffer: ``unsigned int ()(struct tty_struct *tty)``
1211fe18309SJiri Slaby  *
1221fe18309SJiri Slaby  *	This routine returns the number of characters in the device private
1231fe18309SJiri Slaby  *	output queue. Used in tty_wait_until_sent() and for poll()
1241fe18309SJiri Slaby  *	implementation.
1251fe18309SJiri Slaby  *
1261fe18309SJiri Slaby  *	Optional: if not provided, it is assumed there is no queue on the
1271fe18309SJiri Slaby  *	device. Do not call this function directly, call tty_chars_in_buffer().
1281fe18309SJiri Slaby  *
1291fe18309SJiri Slaby  * @ioctl: ``int ()(struct tty_struct *tty, unsigned int cmd,
1301fe18309SJiri Slaby  *		    unsigned long arg)``
1311fe18309SJiri Slaby  *
1321fe18309SJiri Slaby  *	This routine allows the @tty driver to implement device-specific
1331fe18309SJiri Slaby  *	ioctls. If the ioctl number passed in @cmd is not recognized by the
1341fe18309SJiri Slaby  *	driver, it should return %ENOIOCTLCMD.
1351fe18309SJiri Slaby  *
1361fe18309SJiri Slaby  *	Optional.
1371fe18309SJiri Slaby  *
1381fe18309SJiri Slaby  * @compat_ioctl: ``long ()(struct tty_struct *tty, unsigned int cmd,
1391fe18309SJiri Slaby  *			  unsigned long arg)``
1401fe18309SJiri Slaby  *
1411fe18309SJiri Slaby  *	Implement ioctl processing for 32 bit process on 64 bit system.
1421fe18309SJiri Slaby  *
1431fe18309SJiri Slaby  *	Optional.
1441fe18309SJiri Slaby  *
145a8c11c15SIlpo Järvinen  * @set_termios: ``void ()(struct tty_struct *tty, const struct ktermios *old)``
1461fe18309SJiri Slaby  *
1471fe18309SJiri Slaby  *	This routine allows the @tty driver to be notified when device's
14829d5ef68SJiri Slaby  *	termios settings have changed. New settings are in @tty->termios.
14929d5ef68SJiri Slaby  *	Previous settings are passed in the @old argument.
1501fe18309SJiri Slaby  *
15129d5ef68SJiri Slaby  *	The API is defined such that the driver should return the actual modes
15229d5ef68SJiri Slaby  *	selected. This means that the driver is responsible for modifying any
15329d5ef68SJiri Slaby  *	bits in @tty->termios it cannot fulfill to indicate the actual modes
15429d5ef68SJiri Slaby  *	being used.
15529d5ef68SJiri Slaby  *
15629d5ef68SJiri Slaby  *	Optional. Called under the @tty->termios_rwsem. May sleep.
1571fe18309SJiri Slaby  *
1581fe18309SJiri Slaby  * @set_ldisc: ``void ()(struct tty_struct *tty)``
1591fe18309SJiri Slaby  *
1601fe18309SJiri Slaby  *	This routine allows the @tty driver to be notified when the device's
16129d5ef68SJiri Slaby  *	line discipline is being changed. At the point this is done the
16229d5ef68SJiri Slaby  *	discipline is not yet usable.
1631fe18309SJiri Slaby  *
1641fe18309SJiri Slaby  *	Optional. Called under the @tty->ldisc_sem and @tty->termios_rwsem.
1651fe18309SJiri Slaby  *
1661fe18309SJiri Slaby  * @throttle: ``void ()(struct tty_struct *tty)``
1671fe18309SJiri Slaby  *
1681fe18309SJiri Slaby  *	This routine notifies the @tty driver that input buffers for the line
1691fe18309SJiri Slaby  *	discipline are close to full, and it should somehow signal that no more
1701fe18309SJiri Slaby  *	characters should be sent to the @tty.
1711fe18309SJiri Slaby  *
17229d5ef68SJiri Slaby  *	Serialization including with @unthrottle() is the job of the ldisc
17329d5ef68SJiri Slaby  *	layer.
17429d5ef68SJiri Slaby  *
1751fe18309SJiri Slaby  *	Optional: Always invoke via tty_throttle_safe(). Called under the
1761fe18309SJiri Slaby  *	@tty->termios_rwsem.
1771fe18309SJiri Slaby  *
1781fe18309SJiri Slaby  * @unthrottle: ``void ()(struct tty_struct *tty)``
1791fe18309SJiri Slaby  *
1801fe18309SJiri Slaby  *	This routine notifies the @tty driver that it should signal that
1811fe18309SJiri Slaby  *	characters can now be sent to the @tty without fear of overrunning the
1821fe18309SJiri Slaby  *	input buffers of the line disciplines.
1831fe18309SJiri Slaby  *
1841fe18309SJiri Slaby  *	Optional. Always invoke via tty_unthrottle(). Called under the
1851fe18309SJiri Slaby  *	@tty->termios_rwsem.
1861fe18309SJiri Slaby  *
1871fe18309SJiri Slaby  * @stop: ``void ()(struct tty_struct *tty)``
1881fe18309SJiri Slaby  *
1891fe18309SJiri Slaby  *	This routine notifies the @tty driver that it should stop outputting
1901fe18309SJiri Slaby  *	characters to the tty device.
1911fe18309SJiri Slaby  *
1921fe18309SJiri Slaby  *	Called with @tty->flow.lock held. Serialized with @start() method.
1931fe18309SJiri Slaby  *
1941fe18309SJiri Slaby  *	Optional. Always invoke via stop_tty().
1951fe18309SJiri Slaby  *
1961fe18309SJiri Slaby  * @start: ``void ()(struct tty_struct *tty)``
1971fe18309SJiri Slaby  *
1981fe18309SJiri Slaby  *	This routine notifies the @tty driver that it resumed sending
1991fe18309SJiri Slaby  *	characters to the @tty device.
2001fe18309SJiri Slaby  *
2011fe18309SJiri Slaby  *	Called with @tty->flow.lock held. Serialized with stop() method.
2021fe18309SJiri Slaby  *
2031fe18309SJiri Slaby  *	Optional. Always invoke via start_tty().
2041fe18309SJiri Slaby  *
2051fe18309SJiri Slaby  * @hangup: ``void ()(struct tty_struct *tty)``
2061fe18309SJiri Slaby  *
2071fe18309SJiri Slaby  *	This routine notifies the @tty driver that it should hang up the @tty
2081fe18309SJiri Slaby  *	device.
2091fe18309SJiri Slaby  *
2101fe18309SJiri Slaby  *	Optional. Called with tty lock held.
2111fe18309SJiri Slaby  *
2121fe18309SJiri Slaby  * @break_ctl: ``int ()(struct tty_struct *tty, int state)``
2131fe18309SJiri Slaby  *
2141fe18309SJiri Slaby  *	This optional routine requests the @tty driver to turn on or off BREAK
2151fe18309SJiri Slaby  *	status on the RS-232 port. If @state is -1, then the BREAK status
2161fe18309SJiri Slaby  *	should be turned on; if @state is 0, then BREAK should be turned off.
2171fe18309SJiri Slaby  *
2181fe18309SJiri Slaby  *	If this routine is implemented, the high-level tty driver will handle
2191fe18309SJiri Slaby  *	the following ioctls: %TCSBRK, %TCSBRKP, %TIOCSBRK, %TIOCCBRK.
2201fe18309SJiri Slaby  *
2211fe18309SJiri Slaby  *	If the driver sets %TTY_DRIVER_HARDWARE_BREAK in tty_alloc_driver(),
2221fe18309SJiri Slaby  *	then the interface will also be called with actual times and the
2231fe18309SJiri Slaby  *	hardware is expected to do the delay work itself. 0 and -1 are still
2241fe18309SJiri Slaby  *	used for on/off.
2251fe18309SJiri Slaby  *
22629d5ef68SJiri Slaby  *	Optional: Required for %TCSBRK/%BRKP/etc. handling. May sleep.
2271fe18309SJiri Slaby  *
2281fe18309SJiri Slaby  * @flush_buffer: ``void ()(struct tty_struct *tty)``
2291fe18309SJiri Slaby  *
2301fe18309SJiri Slaby  *	This routine discards device private output buffer. Invoked on close,
2311fe18309SJiri Slaby  *	hangup, to implement %TCOFLUSH ioctl and similar.
2321fe18309SJiri Slaby  *
2331fe18309SJiri Slaby  *	Optional: if not provided, it is assumed there is no queue on the
2341fe18309SJiri Slaby  *	device. Do not call this function directly, call
2351fe18309SJiri Slaby  *	tty_driver_flush_buffer().
2361fe18309SJiri Slaby  *
2371fe18309SJiri Slaby  * @wait_until_sent: ``void ()(struct tty_struct *tty, int timeout)``
2381fe18309SJiri Slaby  *
2391fe18309SJiri Slaby  *	This routine waits until the device has written out all of the
2401fe18309SJiri Slaby  *	characters in its transmitter FIFO. Or until @timeout (in jiffies) is
2411fe18309SJiri Slaby  *	reached.
2421fe18309SJiri Slaby  *
2431fe18309SJiri Slaby  *	Optional: If not provided, the device is assumed to have no FIFO.
24429d5ef68SJiri Slaby  *	Usually correct to invoke via tty_wait_until_sent(). May sleep.
2451fe18309SJiri Slaby  *
2461fe18309SJiri Slaby  * @send_xchar: ``void ()(struct tty_struct *tty, char ch)``
2471fe18309SJiri Slaby  *
2481fe18309SJiri Slaby  *	This routine is used to send a high-priority XON/XOFF character (@ch)
2491fe18309SJiri Slaby  *	to the @tty device.
2501fe18309SJiri Slaby  *
2511fe18309SJiri Slaby  *	Optional: If not provided, then the @write method is called under
2521fe18309SJiri Slaby  *	the @tty->atomic_write_lock to keep it serialized with the ldisc.
2531fe18309SJiri Slaby  *
2541fe18309SJiri Slaby  * @tiocmget: ``int ()(struct tty_struct *tty)``
2551fe18309SJiri Slaby  *
2561fe18309SJiri Slaby  *	This routine is used to obtain the modem status bits from the @tty
2571fe18309SJiri Slaby  *	driver.
2581fe18309SJiri Slaby  *
2591fe18309SJiri Slaby  *	Optional: If not provided, then %ENOTTY is returned from the %TIOCMGET
2601fe18309SJiri Slaby  *	ioctl. Do not call this function directly, call tty_tiocmget().
2611fe18309SJiri Slaby  *
2621fe18309SJiri Slaby  * @tiocmset: ``int ()(struct tty_struct *tty,
2631fe18309SJiri Slaby  *		       unsigned int set, unsigned int clear)``
2641fe18309SJiri Slaby  *
2651fe18309SJiri Slaby  *	This routine is used to set the modem status bits to the @tty driver.
2661fe18309SJiri Slaby  *	First, @clear bits should be cleared, then @set bits set.
2671fe18309SJiri Slaby  *
2681fe18309SJiri Slaby  *	Optional: If not provided, then %ENOTTY is returned from the %TIOCMSET
2691fe18309SJiri Slaby  *	ioctl. Do not call this function directly, call tty_tiocmset().
2701fe18309SJiri Slaby  *
2711fe18309SJiri Slaby  * @resize: ``int ()(struct tty_struct *tty, struct winsize *ws)``
2721fe18309SJiri Slaby  *
2731fe18309SJiri Slaby  *	Called when a termios request is issued which changes the requested
2741fe18309SJiri Slaby  *	terminal geometry to @ws.
2751fe18309SJiri Slaby  *
2761fe18309SJiri Slaby  *	Optional: the default action is to update the termios structure
2771fe18309SJiri Slaby  *	without error. This is usually the correct behaviour. Drivers should
2781fe18309SJiri Slaby  *	not force errors here if they are not resizable objects (e.g. a serial
2791fe18309SJiri Slaby  *	line). See tty_do_resize() if you need to wrap the standard method
2801fe18309SJiri Slaby  *	in your own logic -- the usual case.
2811fe18309SJiri Slaby  *
2821fe18309SJiri Slaby  * @get_icount: ``int ()(struct tty_struct *tty,
2831fe18309SJiri Slaby  *			 struct serial_icounter *icount)``
2841fe18309SJiri Slaby  *
2851fe18309SJiri Slaby  *	Called when the @tty device receives a %TIOCGICOUNT ioctl. Passed a
2861fe18309SJiri Slaby  *	kernel structure @icount to complete.
2871fe18309SJiri Slaby  *
2881fe18309SJiri Slaby  *	Optional: called only if provided, otherwise %ENOTTY will be returned.
2891fe18309SJiri Slaby  *
2901fe18309SJiri Slaby  * @get_serial: ``int ()(struct tty_struct *tty, struct serial_struct *p)``
2911fe18309SJiri Slaby  *
2921fe18309SJiri Slaby  *	Called when the @tty device receives a %TIOCGSERIAL ioctl. Passed a
2931fe18309SJiri Slaby  *	kernel structure @p (&struct serial_struct) to complete.
2941fe18309SJiri Slaby  *
2951fe18309SJiri Slaby  *	Optional: called only if provided, otherwise %ENOTTY will be returned.
2961fe18309SJiri Slaby  *	Do not call this function directly, call tty_tiocgserial().
2971fe18309SJiri Slaby  *
2981fe18309SJiri Slaby  * @set_serial: ``int ()(struct tty_struct *tty, struct serial_struct *p)``
2991fe18309SJiri Slaby  *
3001fe18309SJiri Slaby  *	Called when the @tty device receives a %TIOCSSERIAL ioctl. Passed a
3011fe18309SJiri Slaby  *	kernel structure @p (&struct serial_struct) to set the values from.
3021fe18309SJiri Slaby  *
3031fe18309SJiri Slaby  *	Optional: called only if provided, otherwise %ENOTTY will be returned.
3041fe18309SJiri Slaby  *	Do not call this function directly, call tty_tiocsserial().
3051fe18309SJiri Slaby  *
3061fe18309SJiri Slaby  * @show_fdinfo: ``void ()(struct tty_struct *tty, struct seq_file *m)``
3071fe18309SJiri Slaby  *
3081fe18309SJiri Slaby  *	Called when the @tty device file descriptor receives a fdinfo request
3091fe18309SJiri Slaby  *	from VFS (to show in /proc/<pid>/fdinfo/). @m should be filled with
3101fe18309SJiri Slaby  *	information.
3111fe18309SJiri Slaby  *
3121fe18309SJiri Slaby  *	Optional: called only if provided, otherwise nothing is written to @m.
3131fe18309SJiri Slaby  *	Do not call this function directly, call tty_show_fdinfo().
3141fe18309SJiri Slaby  *
3151fe18309SJiri Slaby  * @poll_init: ``int ()(struct tty_driver *driver, int line, char *options)``
3161fe18309SJiri Slaby  *
3171fe18309SJiri Slaby  *	kgdboc support (Documentation/dev-tools/kgdb.rst). This routine is
3181fe18309SJiri Slaby  *	called to initialize the HW for later use by calling @poll_get_char or
3191fe18309SJiri Slaby  *	@poll_put_char.
3201fe18309SJiri Slaby  *
3211fe18309SJiri Slaby  *	Optional: called only if provided, otherwise skipped as a non-polling
3221fe18309SJiri Slaby  *	driver.
3231fe18309SJiri Slaby  *
3241fe18309SJiri Slaby  * @poll_get_char: ``int ()(struct tty_driver *driver, int line)``
3251fe18309SJiri Slaby  *
3261fe18309SJiri Slaby  *	kgdboc support (see @poll_init). @driver should read a character from a
3271fe18309SJiri Slaby  *	tty identified by @line and return it.
3281fe18309SJiri Slaby  *
3291fe18309SJiri Slaby  *	Optional: called only if @poll_init provided.
3301fe18309SJiri Slaby  *
3311fe18309SJiri Slaby  * @poll_put_char: ``void ()(struct tty_driver *driver, int line, char ch)``
3321fe18309SJiri Slaby  *
3331fe18309SJiri Slaby  *	kgdboc support (see @poll_init). @driver should write character @ch to
3341fe18309SJiri Slaby  *	a tty identified by @line.
3351fe18309SJiri Slaby  *
3361fe18309SJiri Slaby  *	Optional: called only if @poll_init provided.
3371fe18309SJiri Slaby  *
3381fe18309SJiri Slaby  * @proc_show: ``int ()(struct seq_file *m, void *driver)``
3391fe18309SJiri Slaby  *
3401fe18309SJiri Slaby  *	Driver @driver (cast to &struct tty_driver) can show additional info in
3411fe18309SJiri Slaby  *	/proc/tty/driver/<driver_name>. It is enough to fill in the information
3421fe18309SJiri Slaby  *	into @m.
3431fe18309SJiri Slaby  *
3441fe18309SJiri Slaby  *	Optional: called only if provided, otherwise no /proc entry created.
3451fe18309SJiri Slaby  *
3461fe18309SJiri Slaby  * This structure defines the interface between the low-level tty driver and
3471fe18309SJiri Slaby  * the tty routines. These routines can be defined. Unless noted otherwise,
3481fe18309SJiri Slaby  * they are optional, and can be filled in with a %NULL pointer.
3491fe18309SJiri Slaby  */
3501da177e4SLinus Torvalds struct tty_operations {
35115f1a633SSukadev Bhattiprolu 	struct tty_struct * (*lookup)(struct tty_driver *driver,
3528ead9dd5SLinus Torvalds 			struct file *filp, int idx);
3538b0a88d5SAlan Cox 	int  (*install)(struct tty_driver *driver, struct tty_struct *tty);
3548b0a88d5SAlan Cox 	void (*remove)(struct tty_driver *driver, struct tty_struct *tty);
3551da177e4SLinus Torvalds 	int  (*open)(struct tty_struct * tty, struct file * filp);
3561da177e4SLinus Torvalds 	void (*close)(struct tty_struct * tty, struct file * filp);
357feebed65SAlan Cox 	void (*shutdown)(struct tty_struct *tty);
358f278a2f7SDave Young 	void (*cleanup)(struct tty_struct *tty);
359*95713967SJiri Slaby (SUSE) 	ssize_t (*write)(struct tty_struct *tty, const u8 *buf, size_t count);
360dcaafbe6SJiri Slaby (SUSE) 	int  (*put_char)(struct tty_struct *tty, u8 ch);
3611da177e4SLinus Torvalds 	void (*flush_chars)(struct tty_struct *tty);
36203b3b1a2SJiri Slaby 	unsigned int (*write_room)(struct tty_struct *tty);
363fff4ef17SJiri Slaby 	unsigned int (*chars_in_buffer)(struct tty_struct *tty);
3646caa76b7SAlan Cox 	int  (*ioctl)(struct tty_struct *tty,
3651da177e4SLinus Torvalds 		    unsigned int cmd, unsigned long arg);
3666caa76b7SAlan Cox 	long (*compat_ioctl)(struct tty_struct *tty,
367e10cc1dfSPaul Fulghum 			     unsigned int cmd, unsigned long arg);
368a8c11c15SIlpo Järvinen 	void (*set_termios)(struct tty_struct *tty, const struct ktermios *old);
3691da177e4SLinus Torvalds 	void (*throttle)(struct tty_struct * tty);
3701da177e4SLinus Torvalds 	void (*unthrottle)(struct tty_struct * tty);
3711da177e4SLinus Torvalds 	void (*stop)(struct tty_struct *tty);
3721da177e4SLinus Torvalds 	void (*start)(struct tty_struct *tty);
3731da177e4SLinus Torvalds 	void (*hangup)(struct tty_struct *tty);
3749e98966cSAlan Cox 	int (*break_ctl)(struct tty_struct *tty, int state);
3751da177e4SLinus Torvalds 	void (*flush_buffer)(struct tty_struct *tty);
3761da177e4SLinus Torvalds 	void (*set_ldisc)(struct tty_struct *tty);
3771da177e4SLinus Torvalds 	void (*wait_until_sent)(struct tty_struct *tty, int timeout);
3781da177e4SLinus Torvalds 	void (*send_xchar)(struct tty_struct *tty, char ch);
37960b33c13SAlan Cox 	int (*tiocmget)(struct tty_struct *tty);
38020b9d177SAlan Cox 	int (*tiocmset)(struct tty_struct *tty,
3811da177e4SLinus Torvalds 			unsigned int set, unsigned int clear);
382fc6f6238SAlan Cox 	int (*resize)(struct tty_struct *tty, struct winsize *ws);
383d281da7fSAlan Cox 	int (*get_icount)(struct tty_struct *tty,
384d281da7fSAlan Cox 				struct serial_icounter_struct *icount);
3852f46a2c1SAl Viro 	int  (*get_serial)(struct tty_struct *tty, struct serial_struct *p);
3862f46a2c1SAl Viro 	int  (*set_serial)(struct tty_struct *tty, struct serial_struct *p);
387d01c3289SMasatake YAMATO 	void (*show_fdinfo)(struct tty_struct *tty, struct seq_file *m);
388f2d937f3SJason Wessel #ifdef CONFIG_CONSOLE_POLL
389f2d937f3SJason Wessel 	int (*poll_init)(struct tty_driver *driver, int line, char *options);
390f2d937f3SJason Wessel 	int (*poll_get_char)(struct tty_driver *driver, int line);
391f2d937f3SJason Wessel 	void (*poll_put_char)(struct tty_driver *driver, int line, char ch);
392f2d937f3SJason Wessel #endif
3931fe18309SJiri Slaby 	int (*proc_show)(struct seq_file *m, void *driver);
3943859a271SKees Cook } __randomize_layout;
3951da177e4SLinus Torvalds 
396a6563830SJiri Slaby /**
397a6563830SJiri Slaby  * struct tty_driver -- driver for TTY devices
398a6563830SJiri Slaby  *
399a6563830SJiri Slaby  * @kref: reference counting. Reaching zero frees all the internals and the
400a6563830SJiri Slaby  *	  driver.
401a6563830SJiri Slaby  * @cdevs: allocated/registered character /dev devices
402a6563830SJiri Slaby  * @owner: modules owning this driver. Used drivers cannot be rmmod'ed.
403a6563830SJiri Slaby  *	   Automatically set by tty_alloc_driver().
404a6563830SJiri Slaby  * @driver_name: name of the driver used in /proc/tty
405a6563830SJiri Slaby  * @name: used for constructing /dev node name
406a6563830SJiri Slaby  * @name_base: used as a number base for constructing /dev node name
407a6563830SJiri Slaby  * @major: major /dev device number (zero for autoassignment)
408a6563830SJiri Slaby  * @minor_start: the first minor /dev device number
409a6563830SJiri Slaby  * @num: number of devices allocated
410a6563830SJiri Slaby  * @type: type of tty driver (%TTY_DRIVER_TYPE_)
411a6563830SJiri Slaby  * @subtype: subtype of tty driver (%SYSTEM_TYPE_, %PTY_TYPE_, %SERIAL_TYPE_)
412a6563830SJiri Slaby  * @init_termios: termios to set to each tty initially (e.g. %tty_std_termios)
413a6563830SJiri Slaby  * @flags: tty driver flags (%TTY_DRIVER_)
414a6563830SJiri Slaby  * @proc_entry: proc fs entry, used internally
415a6563830SJiri Slaby  * @other: driver of the linked tty; only used for the PTY driver
416a6563830SJiri Slaby  * @ttys: array of active &struct tty_struct, set by tty_standard_install()
417a6563830SJiri Slaby  * @ports: array of &struct tty_port; can be set during initialization by
418a6563830SJiri Slaby  *	   tty_port_link_device() and similar
419a6563830SJiri Slaby  * @termios: storage for termios at each TTY close for the next open
420a6563830SJiri Slaby  * @driver_state: pointer to driver's arbitrary data
421a6563830SJiri Slaby  * @ops: driver hooks for TTYs. Set them using tty_set_operations(). Use &struct
422a6563830SJiri Slaby  *	 tty_port helpers in them as much as possible.
423a6563830SJiri Slaby  * @tty_drivers: used internally to link tty_drivers together
424a6563830SJiri Slaby  *
425a6563830SJiri Slaby  * The usual handling of &struct tty_driver is to allocate it by
426a6563830SJiri Slaby  * tty_alloc_driver(), set up all the necessary members, and register it by
427a6563830SJiri Slaby  * tty_register_driver(). At last, the driver is torn down by calling
428a6563830SJiri Slaby  * tty_unregister_driver() followed by tty_driver_kref_put().
429a6563830SJiri Slaby  *
430a6563830SJiri Slaby  * The fields required to be set before calling tty_register_driver() include
431a6563830SJiri Slaby  * @driver_name, @name, @type, @subtype, @init_termios, and @ops.
432a6563830SJiri Slaby  */
4331da177e4SLinus Torvalds struct tty_driver {
434a6563830SJiri Slaby 	struct kref kref;
435a3a10ce3SRichard Watts 	struct cdev **cdevs;
4361da177e4SLinus Torvalds 	struct module	*owner;
4371da177e4SLinus Torvalds 	const char	*driver_name;
4381da177e4SLinus Torvalds 	const char	*name;
439a6563830SJiri Slaby 	int	name_base;
440a6563830SJiri Slaby 	int	major;
441a6563830SJiri Slaby 	int	minor_start;
442a6563830SJiri Slaby 	unsigned int	num;
443a6563830SJiri Slaby 	short	type;
444a6563830SJiri Slaby 	short	subtype;
445a6563830SJiri Slaby 	struct ktermios init_termios;
446a6563830SJiri Slaby 	unsigned long	flags;
447a6563830SJiri Slaby 	struct proc_dir_entry *proc_entry;
448a6563830SJiri Slaby 	struct tty_driver *other;
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds 	/*
4511da177e4SLinus Torvalds 	 * Pointer to the tty data structures
4521da177e4SLinus Torvalds 	 */
4531da177e4SLinus Torvalds 	struct tty_struct **ttys;
45404831dc1SJiri Slaby 	struct tty_port **ports;
455edc6afc5SAlan Cox 	struct ktermios **termios;
456f34d7a5bSAlan Cox 	void *driver_state;
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds 	/*
459f34d7a5bSAlan Cox 	 * Driver methods
4601da177e4SLinus Torvalds 	 */
4611da177e4SLinus Torvalds 
462f34d7a5bSAlan Cox 	const struct tty_operations *ops;
4631da177e4SLinus Torvalds 	struct list_head tty_drivers;
4643859a271SKees Cook } __randomize_layout;
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds extern struct list_head tty_drivers;
4671da177e4SLinus Torvalds 
46878941934SJiri Slaby struct tty_driver *__tty_alloc_driver(unsigned int lines, struct module *owner,
46978941934SJiri Slaby 		unsigned long flags);
47078941934SJiri Slaby struct tty_driver *tty_find_polling_driver(char *name, int *line);
4711da177e4SLinus Torvalds 
47278941934SJiri Slaby void tty_driver_kref_put(struct tty_driver *driver);
473f786ddd2SAdrian Bunk 
4747f0bc6a6SJiri Slaby /* Use TTY_DRIVER_* flags below */
4757f0bc6a6SJiri Slaby #define tty_alloc_driver(lines, flags) \
4767f0bc6a6SJiri Slaby 		__tty_alloc_driver(lines, THIS_MODULE, flags)
4777f0bc6a6SJiri Slaby 
tty_driver_kref_get(struct tty_driver * d)478f786ddd2SAdrian Bunk static inline struct tty_driver *tty_driver_kref_get(struct tty_driver *d)
4797d7b93c1SAlan Cox {
4807d7b93c1SAlan Cox 	kref_get(&d->kref);
4817d7b93c1SAlan Cox 	return d;
4827d7b93c1SAlan Cox }
4837d7b93c1SAlan Cox 
tty_set_operations(struct tty_driver * driver,const struct tty_operations * op)484cb9ea618SJiri Slaby static inline void tty_set_operations(struct tty_driver *driver,
485cb9ea618SJiri Slaby 		const struct tty_operations *op)
486cb9ea618SJiri Slaby {
487cb9ea618SJiri Slaby 	driver->ops = op;
488cb9ea618SJiri Slaby }
489cb9ea618SJiri Slaby 
49034d809f8SJiri Slaby /**
49134d809f8SJiri Slaby  * DOC: TTY Driver Flags
4921da177e4SLinus Torvalds  *
49334d809f8SJiri Slaby  * TTY_DRIVER_RESET_TERMIOS
49434d809f8SJiri Slaby  *	Requests the tty layer to reset the termios setting when the last
49534d809f8SJiri Slaby  *	process has closed the device. Used for PTYs, in particular.
4961da177e4SLinus Torvalds  *
49734d809f8SJiri Slaby  * TTY_DRIVER_REAL_RAW
49834d809f8SJiri Slaby  *	Indicates that the driver will guarantee not to set any special
49934d809f8SJiri Slaby  *	character handling flags if this is set for the tty:
5001da177e4SLinus Torvalds  *
50134d809f8SJiri Slaby  *	``(IGNBRK || (!BRKINT && !PARMRK)) && (IGNPAR || !INPCK)``
5021da177e4SLinus Torvalds  *
50334d809f8SJiri Slaby  *	That is, if there is no reason for the driver to
50434d809f8SJiri Slaby  *	send notifications of parity and break characters up to the line
50534d809f8SJiri Slaby  *	driver, it won't do so.  This allows the line driver to optimize for
50634d809f8SJiri Slaby  *	this case if this flag is set.  (Note that there is also a promise, if
50734d809f8SJiri Slaby  *	the above case is true, not to signal overruns, either.)
5089e98966cSAlan Cox  *
50934d809f8SJiri Slaby  * TTY_DRIVER_DYNAMIC_DEV
51034d809f8SJiri Slaby  *	The individual tty devices need to be registered with a call to
51134d809f8SJiri Slaby  *	tty_register_device() when the device is found in the system and
51234d809f8SJiri Slaby  *	unregistered with a call to tty_unregister_device() so the devices will
51334d809f8SJiri Slaby  *	be show up properly in sysfs.  If not set, all &tty_driver.num entries
51434d809f8SJiri Slaby  *	will be created by the tty core in sysfs when tty_register_driver() is
51534d809f8SJiri Slaby  *	called.  This is to be used by drivers that have tty devices that can
51634d809f8SJiri Slaby  *	appear and disappear while the main tty driver is registered with the
51734d809f8SJiri Slaby  *	tty core.
5189e98966cSAlan Cox  *
51934d809f8SJiri Slaby  * TTY_DRIVER_DEVPTS_MEM
52034d809f8SJiri Slaby  *	Don't use the standard arrays (&tty_driver.ttys and
52134d809f8SJiri Slaby  *	&tty_driver.termios), instead use dynamic memory keyed through the
52234d809f8SJiri Slaby  *	devpts filesystem. This is only applicable to the PTY driver.
5230019b408SJiri Slaby  *
52434d809f8SJiri Slaby  * TTY_DRIVER_HARDWARE_BREAK
52534d809f8SJiri Slaby  *	Hardware handles break signals. Pass the requested timeout to the
52634d809f8SJiri Slaby  *	&tty_operations.break_ctl instead of using a simple on/off interface.
52734d809f8SJiri Slaby  *
52834d809f8SJiri Slaby  * TTY_DRIVER_DYNAMIC_ALLOC
52934d809f8SJiri Slaby  *	Do not allocate structures which are needed per line for this driver
53034d809f8SJiri Slaby  *	(&tty_driver.ports) as it would waste memory. The driver will take
53134d809f8SJiri Slaby  *	care. This is only applicable to the PTY driver.
53234d809f8SJiri Slaby  *
53334d809f8SJiri Slaby  * TTY_DRIVER_UNNUMBERED_NODE
53434d809f8SJiri Slaby  *	Do not create numbered ``/dev`` nodes. For example, create
53534d809f8SJiri Slaby  *	``/dev/ttyprintk`` and not ``/dev/ttyprintk0``. Applicable only when a
53634d809f8SJiri Slaby  *	driver for a single tty device is being allocated.
5371da177e4SLinus Torvalds  */
5381da177e4SLinus Torvalds #define TTY_DRIVER_INSTALLED		0x0001
5391da177e4SLinus Torvalds #define TTY_DRIVER_RESET_TERMIOS	0x0002
5401da177e4SLinus Torvalds #define TTY_DRIVER_REAL_RAW		0x0004
541331b8319SGreg Kroah-Hartman #define TTY_DRIVER_DYNAMIC_DEV		0x0008
5421da177e4SLinus Torvalds #define TTY_DRIVER_DEVPTS_MEM		0x0010
5439e98966cSAlan Cox #define TTY_DRIVER_HARDWARE_BREAK	0x0020
54421aca2faSJiri Slaby #define TTY_DRIVER_DYNAMIC_ALLOC	0x0040
5450019b408SJiri Slaby #define TTY_DRIVER_UNNUMBERED_NODE	0x0080
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds /* tty driver types */
5481da177e4SLinus Torvalds #define TTY_DRIVER_TYPE_SYSTEM		0x0001
5491da177e4SLinus Torvalds #define TTY_DRIVER_TYPE_CONSOLE		0x0002
5501da177e4SLinus Torvalds #define TTY_DRIVER_TYPE_SERIAL		0x0003
5511da177e4SLinus Torvalds #define TTY_DRIVER_TYPE_PTY		0x0004
5521da177e4SLinus Torvalds #define TTY_DRIVER_TYPE_SCC		0x0005	/* scc driver */
5531da177e4SLinus Torvalds #define TTY_DRIVER_TYPE_SYSCONS		0x0006
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds /* system subtypes (magic, used by tty_io.c) */
5561da177e4SLinus Torvalds #define SYSTEM_TYPE_TTY			0x0001
5571da177e4SLinus Torvalds #define SYSTEM_TYPE_CONSOLE		0x0002
5581da177e4SLinus Torvalds #define SYSTEM_TYPE_SYSCONS		0x0003
5591da177e4SLinus Torvalds #define SYSTEM_TYPE_SYSPTMX		0x0004
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds /* pty subtypes (magic, used by tty_io.c) */
5621da177e4SLinus Torvalds #define PTY_TYPE_MASTER			0x0001
5631da177e4SLinus Torvalds #define PTY_TYPE_SLAVE			0x0002
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds /* serial subtype definitions */
5661da177e4SLinus Torvalds #define SERIAL_TYPE_NORMAL	1
5671da177e4SLinus Torvalds 
5684d3d9478SJiri Slaby int tty_register_driver(struct tty_driver *driver);
5694d3d9478SJiri Slaby void tty_unregister_driver(struct tty_driver *driver);
5704d3d9478SJiri Slaby struct device *tty_register_device(struct tty_driver *driver, unsigned index,
5714d3d9478SJiri Slaby 		struct device *dev);
5724d3d9478SJiri Slaby struct device *tty_register_device_attr(struct tty_driver *driver,
5734d3d9478SJiri Slaby 		unsigned index, struct device *device, void *drvdata,
5744d3d9478SJiri Slaby 		const struct attribute_group **attr_grp);
5754d3d9478SJiri Slaby void tty_unregister_device(struct tty_driver *driver, unsigned index);
5764d3d9478SJiri Slaby 
5774d3d9478SJiri Slaby #ifdef CONFIG_PROC_FS
5784d3d9478SJiri Slaby void proc_tty_register_driver(struct tty_driver *);
5794d3d9478SJiri Slaby void proc_tty_unregister_driver(struct tty_driver *);
5804d3d9478SJiri Slaby #else
proc_tty_register_driver(struct tty_driver * d)5814d3d9478SJiri Slaby static inline void proc_tty_register_driver(struct tty_driver *d) {}
proc_tty_unregister_driver(struct tty_driver * d)5824d3d9478SJiri Slaby static inline void proc_tty_unregister_driver(struct tty_driver *d) {}
5834d3d9478SJiri Slaby #endif
5844d3d9478SJiri Slaby 
5851da177e4SLinus Torvalds #endif /* #ifdef _LINUX_TTY_DRIVER_H */
586