summaryrefslogtreecommitdiff
path: root/include/linux/tty_driver.h
blob: f961164a52746728d0d973792efb75d129eb851c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _LINUX_TTY_DRIVER_H
#define _LINUX_TTY_DRIVER_H

#include <linux/export.h>
#include <linux/fs.h>
#include <linux/kref.h>
#include <linux/list.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/termios.h>
#include <linux/seq_file.h>

struct tty_struct;
struct tty_driver;
struct serial_icounter_struct;
struct serial_struct;

/**
 * struct tty_operations -- interface between driver and tty
 *
 * @lookup: ``struct tty_struct *()(struct tty_driver *self, struct file *,
 *				    int idx)``
 *
 *	Return the tty device corresponding to @idx, %NULL if there is not
 *	one currently in use and an %ERR_PTR value on error. Called under
 *	%tty_mutex (for now!)
 *
 *	Optional method. Default behaviour is to use the @self->ttys array.
 *
 * @install: ``int ()(struct tty_driver *self, struct tty_struct *tty)``
 *
 *	Install a new @tty into the @self's internal tables. Used in
 *	conjunction with @lookup and @remove methods.
 *
 *	Optional method. Default behaviour is to use the @self->ttys array.
 *
 * @remove: ``void ()(struct tty_driver *self, struct tty_struct *tty)``
 *
 *	Remove a closed @tty from the @self's internal tables. Used in
 *	conjunction with @lookup and @remove methods.
 *
 *	Optional method. Default behaviour is to use the @self->ttys array.
 *
 * @open: ``int ()(struct tty_struct *tty, struct file *)``
 *
 *	This routine is called when a particular @tty device is opened. This
 *	routine is mandatory; if this routine is not filled in, the attempted
 *	open will fail with %ENODEV.
 *
 *	Required method. Called with tty lock held. May sleep.
 *
 * @close: ``void ()(struct tty_struct *tty, struct file *)``
 *
 *	This routine is called when a particular @tty device is closed. At the
 *	point of return from this call the driver must make no further ldisc
 *	calls of any kind.
 *
 *	Remark: called even if the corresponding @open() failed.
 *
 *	Required method. Called with tty lock held. May sleep.
 *
 * @shutdown: ``void ()(struct tty_struct *tty)``
 *
 *	This routine is called under the tty lock when a particular @tty device
 *	is closed for the last time. It executes before the @tty resources
 *	are freed so may execute while another function holds a @tty kref.
 *
 * @cleanup: ``void ()(struct tty_struct *tty)``
 *
 *	This routine is called asynchronously when a particular @tty device
 *	is closed for the last time freeing up the resources. This is
 *	actually the second part of shutdown for routines that might sleep.
 *
 * @write: ``int ()(struct tty_struct *tty, const unsigned char *buf,
 *		    int count)``
 *
 *	This routine is called by the kernel to write a series (@count) of
 *	characters (@buf) to the @tty device. The characters may come from
 *	user space or kernel space.  This routine will return the
 *	number of characters actually accepted for writing.
 *
 *	May occur in parallel in special cases. Because this includes panic
 *	paths drivers generally shouldn't try and do clever locking here.
 *
 *	Optional: Required for writable devices. May not sleep.
 *
 * @put_char: ``int ()(struct tty_struct *tty, unsigned char ch)``
 *
 *	This routine is called by the kernel to write a single character @ch to
 *	the @tty device. If the kernel uses this routine, it must call the
 *	@flush_chars() routine (if defined) when it is done stuffing characters
 *	into the driver. If there is no room in the queue, the character is
 *	ignored.
 *
 *	Optional: Kernel will use the @write method if not provided. Do not
 *	call this function directly, call tty_put_char().
 *
 * @flush_chars: ``void ()(struct tty_struct *tty)``
 *
 *	This routine is called by the kernel after it has written a
 *	series of characters to the tty device using @put_char().
 *
 *	Optional. Do not call this function directly, call
 *	tty_driver_flush_chars().
 *
 * @write_room: ``unsigned int ()(struct tty_struct *tty)``
 *
 *	This routine returns the numbers of characters the @tty driver
 *	will accept for queuing to be written.  This number is subject
 *	to change as output buffers get emptied, or if the output flow
 *	control is acted.
 *
 *	The ldisc is responsible for being intelligent about multi-threading of
 *	write_room/write calls
 *
 *	Required if @write method is provided else not needed. Do not call this
 *	function directly, call tty_write_room()
 *
 * @chars_in_buffer: ``unsigned int ()(struct tty_struct *tty)``
 *
 *	This routine returns the number of characters in the device private
 *	output queue. Used in tty_wait_until_sent() and for poll()
 *	implementation.
 *
 *	Optional: if not provided, it is assumed there is no queue on the
 *	device. Do not call this function directly, call tty_chars_in_buffer().
 *
 * @ioctl: ``int ()(struct tty_struct *tty, unsigned int cmd,
 *		    unsigned long arg)``
 *
 *	This routine allows the @tty driver to implement device-specific
 *	ioctls. If the ioctl number passed in @cmd is not recognized by the
 *	driver, it should return %ENOIOCTLCMD.
 *
 *	Optional.
 *
 * @compat_ioctl: ``long ()(struct tty_struct *tty, unsigned int cmd,
 *			  unsigned long arg)``
 *
 *	Implement ioctl processing for 32 bit process on 64 bit system.
 *
 *	Optional.
 *
 * @set_termios: ``void ()(struct tty_struct *tty, const struct ktermios *old)``
 *
 *	This routine allows the @tty driver to be notified when device's
 *	termios settings have changed. New settings are in @tty->termios.
 *	Previous settings are passed in the @old argument.
 *
 *	The API is defined such that the driver should return the actual modes
 *	selected. This means that the driver is responsible for modifying any
 *	bits in @tty->termios it cannot fulfill to indicate the actual modes
 *	being used.
 *
 *	Optional. Called under the @tty->termios_rwsem. May sleep.
 *
 * @set_ldisc: ``void ()(struct tty_struct *tty)``
 *
 *	This routine allows the @tty driver to be notified when the device's
 *	line discipline is being changed. At the point this is done the
 *	discipline is not yet usable.
 *
 *	Optional. Called under the @tty->ldisc_sem and @tty->termios_rwsem.
 *
 * @throttle: ``void ()(struct tty_struct *tty)``
 *
 *	This routine notifies the @tty driver that input buffers for the line
 *	discipline are close to full, and it should somehow signal that no more
 *	characters should be sent to the @tty.
 *
 *	Serialization including with @unthrottle() is the job of the ldisc
 *	layer.
 *
 *	Optional: Always invoke via tty_throttle_safe(). Called under the
 *	@tty->termios_rwsem.
 *
 * @unthrottle: ``void ()(struct tty_struct *tty)``
 *
 *	This routine notifies the @tty driver that it should signal that
 *	characters can now be sent to the @tty without fear of overrunning the
 *	input buffers of the line disciplines.
 *
 *	Optional. Always invoke via tty_unthrottle(). Called under the
 *	@tty->termios_rwsem.
 *
 * @stop: ``void ()(struct tty_struct *tty)``
 *
 *	This routine notifies the @tty driver that it should stop outputting
 *	characters to the tty device.
 *
 *	Called with @tty->flow.lock held. Serialized with @start() method.
 *
 *	Optional. Always invoke via stop_tty().
 *
 * @start: ``void ()(struct tty_struct *tty)``
 *
 *	This routine notifies the @tty driver that it resumed sending
 *	characters to the @tty device.
 *
 *	Called with @tty->flow.lock held. Serialized with stop() method.
 *
 *	Optional. Always invoke via start_tty().
 *
 * @hangup: ``void ()(struct tty_struct *tty)``
 *
 *	This routine notifies the @tty driver that it should hang up the @tty
 *	device.
 *
 *	Optional. Called with tty lock held.
 *
 * @break_ctl: ``int ()(struct tty_struct *tty, int state)``
 *
 *	This optional routine requests the @tty driver to turn on or off BREAK
 *	status on the RS-232 port. If @state is -1, then the BREAK status
 *	should be turned on; if @state is 0, then BREAK should be turned off.
 *
 *	If this routine is implemented, the high-level tty driver will handle
 *	the following ioctls: %TCSBRK, %TCSBRKP, %TIOCSBRK, %TIOCCBRK.
 *
 *	If the driver sets %TTY_DRIVER_HARDWARE_BREAK in tty_alloc_driver(),
 *	then the interface will also be called with actual times and the
 *	hardware is expected to do the delay work itself. 0 and -1 are still
 *	used for on/off.
 *
 *	Optional: Required for %TCSBRK/%BRKP/etc. handling. May sleep.
 *
 * @flush_buffer: ``void ()(struct tty_struct *tty)``
 *
 *	This routine discards device private output buffer. Invoked on close,
 *	hangup, to implement %TCOFLUSH ioctl and similar.
 *
 *	Optional: if not provided, it is assumed there is no queue on the
 *	device. Do not call this function directly, call
 *	tty_driver_flush_buffer().
 *
 * @wait_until_sent: ``void ()(struct tty_struct *tty, int timeout)``
 *
 *	This routine waits until the device has written out all of the
 *	characters in its transmitter FIFO. Or until @timeout (in jiffies) is
 *	reached.
 *
 *	Optional: If not provided, the device is assumed to have no FIFO.
 *	Usually correct to invoke via tty_wait_until_sent(). May sleep.
 *
 * @send_xchar: ``void ()(struct tty_struct *tty, char ch)``
 *
 *	This routine is used to send a high-priority XON/XOFF character (@ch)
 *	to the @tty device.
 *
 *	Optional: If not provided, then the @write method is called under
 *	the @tty->atomic_write_lock to keep it serialized with the ldisc.
 *
 * @tiocmget: ``int ()(struct tty_struct *tty)``
 *
 *	This routine is used to obtain the modem status bits from the @tty
 *	driver.
 *
 *	Optional: If not provided, then %ENOTTY is returned from the %TIOCMGET
 *	ioctl. Do not call this function directly, call tty_tiocmget().
 *
 * @tiocmset: ``int ()(struct tty_struct *tty,
 *		       unsigned int set, unsigned int clear)``
 *
 *	This routine is used to set the modem status bits to the @tty driver.
 *	First, @clear bits should be cleared, then @set bits set.
 *
 *	Optional: If not provided, then %ENOTTY is returned from the %TIOCMSET
 *	ioctl. Do not call this function directly, call tty_tiocmset().
 *
 * @resize: ``int ()(struct tty_struct *tty, struct winsize *ws)``
 *
 *	Called when a termios request is issued which changes the requested
 *	terminal geometry to @ws.
 *
 *	Optional: the default action is to update the termios structure
 *	without error. This is usually the correct behaviour. Drivers should
 *	not force errors here if they are not resizable objects (e.g. a serial
 *	line). See tty_do_resize() if you need to wrap the standard method
 *	in your own logic -- the usual case.
 *
 * @get_icount: ``int ()(struct tty_struct *tty,
 *			 struct serial_icounter *icount)``
 *
 *	Called when the @tty device receives a %TIOCGICOUNT ioctl. Passed a
 *	kernel structure @icount to complete.
 *
 *	Optional: called only if provided, otherwise %ENOTTY will be returned.
 *
 * @get_serial: ``int ()(struct tty_struct *tty, struct serial_struct *p)``
 *
 *	Called when the @tty device receives a %TIOCGSERIAL ioctl. Passed a
 *	kernel structure @p (&struct serial_struct) to complete.
 *
 *	Optional: called only if provided, otherwise %ENOTTY will be returned.
 *	Do not call this function directly, call tty_tiocgserial().
 *
 * @set_serial: ``int ()(struct tty_struct *tty, struct serial_struct *p)``
 *
 *	Called when the @tty device receives a %TIOCSSERIAL ioctl. Passed a
 *	kernel structure @p (&struct serial_struct) to set the values from.
 *
 *	Optional: called only if provided, otherwise %ENOTTY will be returned.
 *	Do not call this function directly, call tty_tiocsserial().
 *
 * @show_fdinfo: ``void ()(struct tty_struct *tty, struct seq_file *m)``
 *
 *	Called when the @tty device file descriptor receives a fdinfo request
 *	from VFS (to show in /proc/<pid>/fdinfo/). @m should be filled with
 *	information.
 *
 *	Optional: called only if provided, otherwise nothing is written to @m.
 *	Do not call this function directly, call tty_show_fdinfo().
 *
 * @poll_init: ``int ()(struct tty_driver *driver, int line, char *options)``
 *
 *	kgdboc support (Documentation/dev-tools/kgdb.rst). This routine is
 *	called to initialize the HW for later use by calling @poll_get_char or
 *	@poll_put_char.
 *
 *	Optional: called only if provided, otherwise skipped as a non-polling
 *	driver.
 *
 * @poll_get_char: ``int ()(struct tty_driver *driver, int line)``
 *
 *	kgdboc support (see @poll_init). @driver should read a character from a
 *	tty identified by @line and return it.
 *
 *	Optional: called only if @poll_init provided.
 *
 * @poll_put_char: ``void ()(struct tty_driver *driver, int line, char ch)``
 *
 *	kgdboc support (see @poll_init). @driver should write character @ch to
 *	a tty identified by @line.
 *
 *	Optional: called only if @poll_init provided.
 *
 * @proc_show: ``int ()(struct seq_file *m, void *driver)``
 *
 *	Driver @driver (cast to &struct tty_driver) can show additional info in
 *	/proc/tty/driver/<driver_name>. It is enough to fill in the information
 *	into @m.
 *
 *	Optional: called only if provided, otherwise no /proc entry created.
 *
 * This structure defines the interface between the low-level tty driver and
 * the tty routines. These routines can be defined. Unless noted otherwise,
 * they are optional, and can be filled in with a %NULL pointer.
 */
struct tty_operations {
	struct tty_struct * (*lookup)(struct tty_driver *driver,
			struct file *filp, int idx);
	int  (*install)(struct tty_driver *driver, struct tty_struct *tty);
	void (*remove)(struct tty_driver *driver, struct tty_struct *tty);
	int  (*open)(struct tty_struct * tty, struct file * filp);
	void (*close)(struct tty_struct * tty, struct file * filp);
	void (*shutdown)(struct tty_struct *tty);
	void (*cleanup)(struct tty_struct *tty);
	int  (*write)(struct tty_struct * tty,
		      const unsigned char *buf, int count);
	int  (*put_char)(struct tty_struct *tty, unsigned char ch);
	void (*flush_chars)(struct tty_struct *tty);
	unsigned int (*write_room)(struct tty_struct *tty);
	unsigned int (*chars_in_buffer)(struct tty_struct *tty);
	int  (*ioctl)(struct tty_struct *tty,
		    unsigned int cmd, unsigned long arg);
	long (*compat_ioctl)(struct tty_struct *tty,
			     unsigned int cmd, unsigned long arg);
	void (*set_termios)(struct tty_struct *tty, const struct ktermios *old);
	void (*throttle)(struct tty_struct * tty);
	void (*unthrottle)(struct tty_struct * tty);
	void (*stop)(struct tty_struct *tty);
	void (*start)(struct tty_struct *tty);
	void (*hangup)(struct tty_struct *tty);
	int (*break_ctl)(struct tty_struct *tty, int state);
	void (*flush_buffer)(struct tty_struct *tty);
	void (*set_ldisc)(struct tty_struct *tty);
	void (*wait_until_sent)(struct tty_struct *tty, int timeout);
	void (*send_xchar)(struct tty_struct *tty, char ch);
	int (*tiocmget)(struct tty_struct *tty);
	int (*tiocmset)(struct tty_struct *tty,
			unsigned int set, unsigned int clear);
	int (*resize)(struct tty_struct *tty, struct winsize *ws);
	int (*get_icount)(struct tty_struct *tty,
				struct serial_icounter_struct *icount);
	int  (*get_serial)(struct tty_struct *tty, struct serial_struct *p);
	int  (*set_serial)(struct tty_struct *tty, struct serial_struct *p);
	void (*show_fdinfo)(struct tty_struct *tty, struct seq_file *m);
#ifdef CONFIG_CONSOLE_POLL
	int (*poll_init)(struct tty_driver *driver, int line, char *options);
	int (*poll_get_char)(struct tty_driver *driver, int line);
	void (*poll_put_char)(struct tty_driver *driver, int line, char ch);
#endif
	int (*proc_show)(struct seq_file *m, void *driver);
} __randomize_layout;

/**
 * struct tty_driver -- driver for TTY devi