linux采用模塊方法,添加一個新的設備


該文轉載自:http://rangercyh.blog.51cto.com/1444712/521244

系統調用是操作系統內核和應用程序之間的接口,而設備驅動程序是操作系統內核和機器硬件之間的接口。

設備驅動程序為應用程序屏蔽了硬件的細節,這樣在應用程序看來,硬件設備只是一個設備文件, 應用程序可以像操作普通文件一樣對硬件設備進行操作。

設備驅動程序是內核的一部分,它完成以下的功能:

(1) 對設備初始化和釋放.

(2) 把數據從內核傳送到硬件和從硬件讀取數據.

(3) 讀取應用程序傳送給設備文件的數據和回送應用程序請求的數據.

(4) 檢測和處理設備出現的錯誤.

Linux支持三中不同類型的設備:字符設備(character devices)、塊設備(block devices)和網絡設備(network interfaces)。

字符設備和塊設備的主要區別是:在對字符設備發出讀/寫請求時,實際的硬件I/O一般就緊接着發生了。

塊設備則不然,它利用一塊系統內存作緩沖區,當用戶進程對設備請求能滿足用戶的要求,就返回請求的數據,如果不能,就調用請求函數來進行實際的I/O操作。塊設備是主要針對磁盤等慢速設備設計的,以免耗費過多的CPU時間來等待.

用戶進程是通過設備文件來與實際的硬件打交道,每個設備文件都都有其文件屬性(c/b),表示是字符設備還是塊設備。

另外每個文件都有兩個設備號,第一個是主設備號,標識驅動程序,第二個是從設備號,標識使用同一個設備驅動程序的不同的硬件設備

比如有兩個軟盤,就可以用從設備號來區分他們.設備文件的的主設備號必須與設備驅動程序在登記時申請的主設備號一致,否則用戶進程將無法訪問到驅動程序.。

 

設備驅動程序工作的基本原理:

用戶進程利用系統調用對設備進行諸如read/write操作,系統調用通過設備文件的主設備號找到相應的設備驅動程序,然后讀取這個數據結構相應的函數指針,接着把控制權交給該函數。

最后,在用戶進程調用驅動程序時,系統進入核心態,這時不再是搶先式調度。也就是說,系統必須在你的驅動程序的子函數返回后才能進行其他的工作。

如果你的驅動程序陷入死循環,你只有重新啟動機器了。

 

下面我們就來添加一個字符設備:

  • 編寫設備驅動源代碼

在設備驅動程序中有一個非常重要的結構file_operations,該結構的每個域都對應着一個系統調用。

用戶進程利用系統調用對設備文件進行諸如read/write操作時,系統調用通過設備文件的主設備號找到相應的設備驅動程序,然后讀取這個數據結構相應的函數指針,接着把控制權交給該函數。

  1. struct file_operations { 
  2. int (*seek) (struct inode * ,struct file *, off_t ,int);  
  3. int (*read) (struct inode * ,struct file *, char ,int);  
  4. int (*write) (struct inode * ,struct file *, off_t ,int);  
  5. int (*readdir) (struct inode * ,struct file *, struct dirent * ,int);  
  6. int (*select) (struct inode * ,struct file *, int ,select_table *);  
  7. int (*ioctl) (struct inode * ,struct file *, unsined int ,unsigned long);  
  8. int (*mmap) (struct inode * ,struct file *, struct vm_area_struct *);  
  9. int (*open) (struct inode * ,struct file *);  
  10. int (*release) (struct inode * ,struct file *);  
  11. int (*fsync) (struct inode * ,struct file *);  
  12. int (*fasync) (struct inode * ,struct file *,int);  
  13. int (*check_media_change) (struct inode * ,struct file *);  
  14. int (*revalidate) (dev_t dev);  

編寫設備驅動程序的主要工作是編寫子函數,並填充file_operations的各個域。

例如:

  1. Struct file_operations my_fops={ 
  2. .read=my_read, 
  3. .write=my_write, 
  4. .open=my_open, 
  5. .release=my_release 

然后再定義函數my_read,my_write,my_open,my_release相應的函數體。

例如:

  1. static ssize_t my_open(struct inode *inode,struct file *file){ 
  2. static int counter=0; 
  3. if(Device_Open) 
  4. return -EBUSY; 
  5. Device_Open++; 
  6. /*寫入設備的信息*/ 
  7. sprintf(msg,"the device has been called %d times\n",counter++); 
  8. msg_ptr=msg; 
  9. return 0; 

同時對於可卸載的內核模塊(LKM),至少還有兩個基本的模塊:

例如本例中的:

  1. static int __init my_init(void){ 
  2. int result; 
  3. result=register_chrdev(0,"sky_driver",&my_fops); 
  4. if(result<0){ 
  5. printk("error:can not register the device\n"); 
  6. return -1; 
  7. if(my_major==0){ 
  8. my_major=result; 
  9. printk("&lt;1>hehe,the device has been registered!\n"); 
  10. printk("<1>the virtual device was assigned major number %d.\n",my_major); 
  11. printk("<1>To talk to the driver,create a dev file with\n"); 
  12. printk("<1>'mknod/dev/my c %d 0'\n",my_major); 
  13. printk("<1>Remove the dev and the file when done\n"); 
  14. return 0; 
  15.  
  16. static void __exit my_exit(void){ 
  17. unregister_chrdev(my_major,"sky_driver"); 
  18. printk("<1>unloading the device\n"); 

my_init 用於注冊設備,獲得設備的主設備號

調用register_chrdev(0,“sky_driver(設備名)”,&my_fops);

my_exit 用於注銷設備

調用unregister_chrdev(my_major, “sky_driver(設備名)”);

然后在程序尾再調用這兩個函數

Module_init(my_init);

Module_exit(my_exit)

MODULE_LICENSE(“GPL”);

 

編寫自己的驅動程序源文件mydriver.c:

  1. #include <linux/kernel.h> 
  2. #include <linux/module.h> 
  3. #include <linux/fs.h> 
  4. #include <linux/init.h> 
  5. #include <linux/uaccess.h> 
  6.  
  7. #if CONFIG_MODVERSIONS == 1 
  8. #define MODVERSIONS 
  9. #include <linux/version.h> 
  10. #endif 
  11.  
  12. #define DEVICE_NUM 0 //隨機產生一個設備號 
  13.  
  14. static int device_num = 0;  //用來保存創建成功后的設備號 
  15. static char buffer[1024] = "mydriver";   //數據緩沖區     
  16. static int open_nr = 0;     //打開設備的進程數,用於內核的互斥 
  17.  
  18. //函數聲明 
  19. static int mydriver_open(struct inode *inode, struct file *filp); 
  20. static int mydriver_release(struct inode *inode, struct file* filp); 
  21. static ssize_t mydriver_read(struct file *file, char __user *buf, size_t count, loff_t *f_pos); 
  22. static ssize_t mydriver_write(struct file *file, const char __user *buf, size_t count, loff_t *f_pos); 
  23.  
  24. //填充file_operations結構相關入口 
  25. static struct file_operations mydriver_fops = { 
  26.     .read    = mydriver_read, 
  27.     .write   = mydriver_write, 
  28.     .open    = mydriver_open, 
  29.     .release = mydriver_release, 
  30. };    
  31.  
  32. //打開函數 
  33. static int mydriver_open(struct inode *inode, struct file *filp) 
  34.     printk("\nMain device is %d, and the slave device is %d\n", MAJOR(inode->i_rdev), MINOR(inode->i_rdev)); 
  35.     if (open_nr == 0) { 
  36.         open_nr++; 
  37.         try_module_get(THIS_MODULE); 
  38.             return 0; 
  39.     }  
  40.     else { 
  41.             printk(KERN_ALERT "Another process open the char device.\n");//進程掛起 
  42.             return -1; 
  43.         }  
  44.  
  45. //讀函數 
  46. static ssize_t mydriver_read(struct file *file, char __user *buf, size_t count, loff_t *f_pos) 
  47.     //if (buf == NULL) return 0; 
  48.     if (copy_to_user(buf, buffer, sizeof(buffer))) //讀緩沖 
  49.     { 
  50.         return -1; 
  51.     } 
  52.     return sizeof(buffer); 
  53.  
  54. //寫函數,將用戶的輸入字符串寫入 
  55. static ssize_t mydriver_write(struct file *file, const char __user *buf, size_t count, loff_t *f_pos) 
  56.     //if (buf == NULL) return 0; 
  57.     if (copy_from_user(buffer, buf, sizeof(buffer))) //寫緩沖 
  58.     { 
  59.         return -1; 
  60.     } 
  61.     return sizeof(buffer); 
  62.  
  63. //釋放設備函數 
  64. static int mydriver_release(struct inode *inode, struct file* filp) 
  65.     open_nr--; //進程數減1 
  66.     printk("The device is released!\n"); 
  67.     module_put(THIS_MODULE); 
  68.         return 0; 
  69.  
  70. //注冊設備函數 
  71. static int __init mydriver_init(void) 
  72.     int result; 
  73.  
  74.     printk(KERN_ALERT "Begin to init Char Device!"); //注冊設備 
  75.     //向系統的字符登記表登記一個字符設備 
  76.     result = register_chrdev(DEVICE_NUM, "mydriver", &mydriver_fops); 
  77.  
  78.     if (result < 0) { 
  79.         printk(KERN_WARNING "mydriver: register failure\n"); 
  80.         return -1; 
  81.     } 
  82.     else { 
  83.         printk("mydriver: register success!\n"); 
  84.         device_num = result; 
  85.         return 0; 
  86.     } 
  87.  
  88. //注銷設備函數 
  89. static void __exit mydriver_exit(void) 
  90.     printk(KERN_ALERT "Unloading...\n"); 
  91.     unregister_chrdev(device_num, "mydriver"); //注銷設備 
  92.     printk("unregister success!\n"); 
  93.  
  94. //模塊宏定義 
  95. module_init(mydriver_init); 
  96. module_exit(mydriver_exit); 
  97.  
  98. MODULE_LICENSE("GPL"); 
  • 編譯該設備驅動代碼

然后將設備驅動源文件復制到/usr/src/linux/drivers/misc下

image

修改misc目錄下的Makefile文件,只要在最后添加一句即可:obj-m +=mydriver.o。

image

/usr/src/linux/drivers/misc路徑下執行命令:Make -C /usr/src/linux SUBDIRS=$PWD modules編譯成功將得到mydriver.ko文件。

image

可以在misc目錄下觀察得到了mydriver.ko文件。

image

繼續執行insmod ./mydriver.ko命令掛載內核中的模塊。

然后通過lsmod命令可以看到增加的設備模塊mydriver。

image

輸入cat /var/log/messages可以看到設備注冊成功。

image

此時進入/proc/devices文件會看到在字符設備中有250 mydriver。前面的是系統分配的主設備號,后面是設備注冊名。

進入在/dev路徑下,執行命令:

mknod /dev/mydriver c 250 0

第一個參數是新建設備文件的地址和名字。

第二個參數是指創建的是字符設備文件。

第三個參數是主設備號,第四個參數是從設備號,自己隨便取。

執行成功會在/dev/char中看到一個新的設備文件mydriver

image

至此設備添加成功。

  • 編譯測試程序。

編寫測試代碼如下:

  1. #include <sys/types.h> 
  2. #include <sys/stat.h> 
  3. #include <string.h> 
  4. #include <stdio.h> 
  5. #include <fcntl.h> 
  6.  
  7. int main(void) 
  8.     int fd; 
  9.     char buf[1024]; 
  10.     char get[1024]; 
  11.  
  12.     memset(get, 0, sizeof(get)); 
  13.     memset(buf, 0, sizeof(buf)); 
  14.     printf("please enter a string you want input to mydriver:\n"); 
  15.     gets(get); 
  16.  
  17.     fd = open("/dev/mydriver", O_RDWR, S_IRUSR|S_IWUSR);//打開設備 
  18.  
  19.     if (fd > 0) { 
  20.         read(fd, &buf, sizeof(buf)); 
  21.         printf("The message in mydriver now is: %s\n", buf); 
  22.  
  23.         //將輸入寫入設備 
  24.         write(fd, &get, sizeof(get)); 
  25.         //讀出設備的信息並打印 
  26.         read(fd, &buf, sizeof(buf)); 
  27.         printf("The message changed to: %s\n", buf); 
  28.         sleep(1); 
  29.     }  
  30.     else { 
  31.         printf("OMG..."); 
  32.         return -1; 
  33.     } 
  34.  
  35.     close(fd);//釋放設備 
  36.  
  37.     return 0; 

gcc -o mydriver_test mydriver_test.c

./mydriver_test

image

輸入任意字符串,驅動程序將字符串拷貝進新加入的設備,然后再讀取出來,設備中保留字符串信息,再次輸入將覆蓋原來的信息


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM