tiny4412 串口驱动分析九 --- shell终端
作者:彭東林
郵箱:pengdonglin137@163.com
?
開發板:tiny4412ADK+S700 4GB Flash
主機:Wind7 64位
虛擬機:Vmware+Ubuntu12_04
u-boot:U-Boot 2010.12
Linux內核版本:linux-3.0.31
Android版本:android-4.1.2
?
?
在上面我們知道了/dev/ttySACx是如何生成的,此外還可以看到在/dev下還有設備結點/dev/console,以及/dev/tty等設備結點。
?
可以看到向/dev/ttySAC0、/dev/console和/dev/tty輸入字符,然后這些字符會輸出到串口終端上:
?
但是如果使用adb shell登陸后,現象不同:
其中,左邊的窗口是在adb shell下,右邊的窗口是串口終端的顯示,可以看到如果在adb shell下向 /dev/ttySAC0和 /dev/console下寫入字符的話,這個字符并沒有在adb shell終端下顯示,相反卻在串口終端中顯示出來,當在adb shell終端下向/dev/tty下寫入字符時,就在adb shell終端下顯示出來了,并沒有影響到串口終端的顯示。
上面的這些現象背后的原因是什么呢?下面我們開始分析內核源碼來解釋。
首先需要知道這些設備結點是怎么生成的:
late_initcall(chr_dev_init);
???? ---- tty_init()?? (drivers/tty/tty_io.c)
int __init tty_init(void) {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(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL, "tty");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");consdev = device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), NULL,"console");if (IS_ERR(consdev))consdev = NULL;elseWARN_ON(device_create_file(consdev, &dev_attr_active) < 0);return 0; }在這里會在/dev/下生成console和tty兩個設備結點,他們對應的fops分別是tty_fops和console_fops。
在上面分析的tty_register_device函數會生成/dev/ttySACx,它對應的fops在函數tty_register_driver中設置為了tty_fops。所以這里的關鍵是分析tty_fops和console_fops是如何實現的。
對比發現,console_fops和tty_fops是一樣的:
static const struct file_operations tty_fops = {.llseek = no_llseek,.read = tty_read,.write = tty_write,.poll = tty_poll,.unlocked_ioctl = tty_ioctl,.compat_ioctl = tty_compat_ioctl,.open = tty_open,.release = tty_release,.fasync = tty_fasync, };static const struct file_operations console_fops = {.llseek = no_llseek,.read = tty_read,.write = redirected_tty_write,.poll = tty_poll,.unlocked_ioctl = tty_ioctl,.compat_ioctl = tty_compat_ioctl,.open = tty_open,.release = tty_release,.fasync = tty_fasync, };執行echo “peng” > /dev/ttySAC0的時候,會先調用tty_open然后調用tty_write,最后調用tty_release。
?
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的tiny4412 串口驱动分析九 --- shell终端的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IOS开发-GitHub使用详解
- 下一篇: 排序和顺序统计学(2)——快速排序