- Brief introduction
- The name of the TTY device is abbreviated from the previous telex, originally referred to as a physical or virtual terminal connected to a UNIX system
- The core of the Linux TTY driver is immediately below the standard character device driver layer and provides a range of functions that are used as interfaces by terminal type devices
- There are three types of TTY drivers: console, serial, and Pty
- /proc/tty/drivers
- TTY devices that are currently registered and are in the kernel have their own subdirectories under/sys/class/tty
- Small TTY drivers
- <linux/tty_driver.h>
- struct Tty_driver
- Tiny_tty_driver = Alloc_tty_driver (tiny_tty_minors);
- static struct Tty_operations Serial_ops = {. Open=tiny_open,. Close=tiny_close,. Write=tiny_write,. Write_room=tiny_ Write_room,. Set_termios=tiny_set_termios,}
- tiny_tty_driver->owner=this_module;
- Tiny_tty_driver->driver_name= "Tiny_tty";
- Tiny_tty_driver->name= "Ttty";
- Tiny_tty_driver->devfs_name= "tty/ttty%d";
- tiny_tty_driver->major=tiny_tty_major;
- tiny_tty_driver->type=tty_driver_type_serial;
- tiny_tty_driver->subtype=serial_type_normal;
- tiny_tty_driver->flags=tty_driver_real_raw| TTY_DRIVER_NO_DEVFS;
- tiny_tty_driver->init_termios=tty_std_termios;
- tiny_tty_driver->init_termios.c_cflag=b9600| cs8| cread| hupcl| clocal;
- Tty_set_operaions (Tiny_tty_driver, &serial_ops);
- retval = Tty_register_driver (tiny_tty_driver);
- for (i=0;i<tiny_tty_minors; ++i) tty_unregister_device (Tiny_tty_driver, i);
- Tty_unregister_driver (Tiny_tty_driver);
- Termios structure
- Used to provide a set of safe setting values
- struct Termios
- tcflag_t C_iflag;
- tcflag_t C_oflag;
- tcflag_t C_cflag;
- Tcflag_c C_lflag;
- cc_t C_line;
- tty_driver function pointer
- open and close
- when a user opens a device node assigned by a driver using open, the TTY core calls the Open function
- when the open function is called The TTY driver either saves the data to the TTY_STRUCT variable passed to it
- Data Flow
- when the data is being sent to the hardware, the user calls the Write function
- first TTY core When the call is received and the kernel sends the data to the write function of the TTY driver, the
- TTY driver does not invoke any function that might hibernate when it is in the context of the interrupt
- when the TTY subsystem itself needs to transfer some data outside the TTY device, You can call the Write function
- when the TTY core wants to know the available write buffer size provided by the TTY driver, the Write_room function is called
- additional buffering functions
- F Lush_chars
- wait_until_sent
- flush_buffer
- How is there no read function
- when TTY Once the driver receives the data, it is responsible for passing any data obtained from the hardware to the TTY core without using the traditional read function
- TTY core to buffer the data until it receives a request from the user
- in a named Tty_flip_ In the structure of buffer, the TTY core buffers the data received from the TTY driver
- tty_insert_flip_char
- tty_flip_buffer_push
- TTY Line Settings
- set_termios
- Most termios user-space functions will be converted to the IOCTL call to the driver node by the library
- a large number of different TTY IOCTL calls will The Set_termios function called by the Ttyp core into a TTY driver must be able to decode all the different settings in the Termios structure and respond to any required changes
tiocmget and Tiocmset
- in 2.4 and earlier kernels, a large number of TTY IOCTL calls were used to obtain and set different control line parameters
- This is done through constants Tiocmget, Tiocmbis, TIOCM BIC and Tiocmset to complete the line setting values used by the
- tiocmget to get the kernel, in the 2.6. Version of the kernel, the IOCTL call is replaced by the Tiocmget callback function in the TTY driver
- The remaining three ioctl are now simplified to a tiocmset callback function in the TTY driver
- Int (*tiocmget) (struct tty_struct *tty, struct file *file);
- Int (*tiocmset) (struct tty_struct *tty, struct file *file, unsigned int set, unsigned int clear);
- IOCTLs
- When the IOCTL is called for a device node, the TTY core calls the IOCTL callback function in the TTY_DRIVER structure
- List of common word TTY IOCTL
- Tiocsergetlsr
- Get the value of this TTY device line status register (LSR)
- Tiocgserial
- Get Serial line information
- Tiocsserial
- Set up Serial line information
- Tiocmiwait
- Wait for the MSR to change
- Tiocgicount
- Proc and sys handling of TTY devices
- The TTY core provides a very simple way for any TTY driver to maintain a file in the/proc/tty/driver directory
- If the driver defines a read_proc or Write_proc function, the file is created, and any read/write to the file is sent to the driver
- Tty_driver structure Detailed
- The tty_driver structure is used to register a TTY driver with the TTY core
- struct Tty_driver
- struct module *owner;
- int magic;
- const char *driver_name;
- Used in/proc/tty and SYSFS, indicating the name of the driver
- const char *name;
- The name of the driver node
- int name_base;
- The start number used to create the device name
- Short major;
- Driver's main device number
- Short Minor_start;
- Minimum secondary device number used by the driver
- Short num;
- The number of secondary device numbers that can be assigned to the driver
- Short type;
- Tty_driver_type_system
- Tty_driver_type_console
- Tty_driver_type_serial
- Tty_driver_type_pty
- Short subtype;
- Describes what TTY drivers are registered with the TTY core
- struct Termios Init_termios;
- When created, the TERMIOS structure containing the initial value
- int flags;
- struct Proc_dir_entry *proc_entry;
- The driver's/PROC entry structure body
- struct Tty_driver *other;
- Pointers to TTY slave device drivers
- void *driver_state;
- Status inside the TTY driver
- struct Tty_driver *next;
- struct Tty_driver *prev;
- Tty_operations structure Detailed
- The tty_operations structure contains all the callback functions, which are set by the TTY driver and are called by the TTY core
- struct tty_operations
- Int (*open) (struct tty_struct *tty, struct file *filp);
- Int (*close) (struct tty_struct *tty, struct file *filp);
- Int (*write) (struct tty_struct *tty, const unsigned char *buf, int count);
- void (*put_char) (struct tty_struct *tty, unsigned char ch);
- void (*flush_chars) (struct tty_struct *tty);
- void (*wait_until_sent) (struct tty_struct *tty, int timeout);
- Int (*write_room) (struct tty_struct *tty);
- Int (*chars_in_buffer) (struct tty_struct *tty);
- Int (*ioctl) (struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
- void (*set_termios) (struct tty_struct *tty, struct termios *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);
- void (*bread_ctl) (struct tty_struct *tty, int state);
- void (*flush_buffer) (struct tty_struct *tty);
- void (*set_ldisc) (struct tty_struct *tty);
- void (*send_xchar) (struct tty_struct *tty, char ch);
- Int (*read_proc) (Char *page, Char **start, off_t off, int count, int *eof, void *data);
- Int (*write_proc) (struct file *file, const char *buffer, unsigned long count, void *data);
- Int (*tiocmget) (struct tty_struct *tty, struct file *file);
- Int (*tiocset) (struct tty_struct *tty, struct file *file, unsigned int set, unsigned int clear);
- TTY_STRUCT structure Detailed
- TTY Core uses tty_struct to save the status of the current specific TTY port
- struct TTY_STRUCT
- unsigned long flags;
- Status of the current TTY device
- Tty_throttled
- Tty_io_error
- Tty_other_closed
- Tty_exclusive
- Tty_debug
- Tty_do_write_wakeup
- Tty_push
- Tty_closing
- Tty_dont_flip
- Tty_hw_cook_out
- Tty_hw_cook_in
- Tty_pty_lock
- Tty_no_write_split
- struct Tty_flip_buffer flip;
- Alternate buffers for TTY devices
- struct Tty_ldisc ldisc;
- Line code for TTY devices
- wait_queue_head_t write_wait;
- Wait_queue for TTY write functions
- struct Termios *termios;
- Pointer to the TERMIOS structure that sets the TTY device
- unsigned char stopped:1;
- Indicates whether the TTY device has stopped
- unsigned char hw_stopped:1
- Indicates whether the TTY device hardware has stopped
- unsigned char low_latency:1
- Indicates if the TTY device is a slow device
- unsigned char closing:1
- Indicates whether the TTY device is shutting down the port
- struct Tty_driver driver;
- Control the current tty_driver structure of a TTY device
- void *driver_data;
- Tty_driver pointers used to store data in the TTY driver
"Linux Device Drivers" 18th TTY driver--note