Linux tty core 源码分析
Linux tty core 源码分析
[日期:2011-02-08]来源:CSDN? 作者:sirzjp本文以linux 2.6.27内核为基础,阅读tty core 源码并作注解,自己接触时间不长,希望与爱好者共同分享,错误之处还望指正。
linux tty core 是建立在字符设备驱动的基础之上,并为tty类型设备(串口、控制台、虚拟终端)提供一个公用的平台。所以任何一个tty设备驱动的注册都是作为一个字符设备驱动而操作的。下面我们看看代码中是如何处理的:
/* 3/2004 jmc: why do these devices exist? */
//tty核心默认在内核中实现的字符型tty设备驱动
static struct cdev tty_cdev, console_cdev;#ifdef CONFIG_UNIX98_PTYSstatic struct cdev ptmx_cdev;#endif#ifdef CONFIG_VTstatic struct cdev vc0_cdev;#endif
/*?* Ok, now we can initialize the rest of the tty devices and can count?* on memory allocations, interrupts etc..?*/static int __init tty_init(void){
//在字符设备模型中加入注册tty_cdev驱动并加入/dev/tty这样的设备
?cdev_init(&tty_cdev, &tty_fops);?if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||???? register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)? panic("Couldn't register /dev/tty driver\n");?device_create_drvdata(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL,???????? "tty");
//在字符设备模型中加入注册console_cdev驱动并加入/dev/console这样的设备
?cdev_init(&console_cdev, &console_fops);?if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||???? register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)? panic("Couldn't register /dev/console driver\n");?device_create_drvdata(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), NULL,???????? "console");
//在字符设备模型中加入注册ptmx_cdev驱动并加入/dev/ptmx这样的设备
#ifdef CONFIG_UNIX98_PTYS?cdev_init(&ptmx_cdev, &ptmx_fops);?if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||???? register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)? panic("Couldn't register /dev/ptmx driver\n");?device_create_drvdata(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");#endif
//在字符设备模型中加入注册vc0_cdev驱动并加入/dev/tty0这样的设备
#ifdef CONFIG_VT?cdev_init(&vc0_cdev, &console_fops);?if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||???? register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)? panic("Couldn't register /dev/tty0 driver\n");?device_create_drvdata(tty_cl