字符設備驅動

(1)、實現功能:創建設備文件。在用戶空間實現對設備文件的操作。

(2)、實現過程:

寫字符設備程序char_1.c 文件==》寫makefile並編譯==》加載$insmod char_1.ko ==》查看系統的設備號$cat  /proc/devices (已經爲名爲xtl_test的設備申請了一個設備號 111

1、寫字符設備程序char_1.c 文件

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>

MODULE_LICENSE("Dual BSD/GPL");
unsigned int major=111;
struct file_operations t_fops;

static int xtl_init(void)
{
        printk("come in kerner....\n");
        register_chrdev(major,"xtl_test",&t_fops);
        return 0;
}

static void xtl_exit(void)
{
        unregister_chrdev(major,"xtl_test");
        printk("leave kernel...\n");
        return;
}

module_init(xtl_init);
2、 寫makefile並編譯==》加載$insmod char_1.ko 

3、查看系統的設備號$cat  /proc/devices  (已經爲名爲xtl_test 的設備申請了一個設備號 111)

4、$ mknod  xtl_test    c   111  0    ( 在/dev 下創建一個設備文件)

5、在用戶空間實現對設備文件的open 、close.

今天對設備文件進行了open 和close 操作,read 的操作中不能理解用戶的程序和內核的程序怎麼聯繫到一起的,對於內核中的函數的調用查閱 http://lxr.linux.no/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章