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