Linux下的第一个驱动程序(模板)


makefile文件:

  1. KERN_DIR = /home/youshan/linux-2.6.32.2  
  2.   
  3. all:  
  4.         make -C $(KERN_DIR) M=`pwd` modules   
  5.   
  6. clean:  
  7.         make -C $(KERN_DIR) M=`pwd` modules clean  
  8.         rm -rf modules.order  
  9.   
  10. obj-m   += first_drv.o  
驱动程序:
  1. /************************************************* 
  2. * File name   : first_drv.c(可当做模板使用) 
  3. * Description : 参考韦东山视频,改写了部分代码 
  4. * Author      : sg131971@qq.com 
  5. * Version     : V1.0 
  6. * Date        :  
  7. * Compiler    : arm-linux-gcc-4.4.3 
  8. * Target      : mini2440(Linux-2.6.32) 
  9. * History     :  
  10. *   <author>  <time>   <version >   <desc> 
  11. *************************************************/  
  12. #include <linux/module.h>   
  13. #include <linux/types.h>   
  14. #include <linux/fs.h>   
  15. #include <linux/errno.h>   
  16. #include <linux/mm.h>   
  17. #include <linux/sched.h>   
  18. #include <linux/init.h>   
  19. #include <linux/cdev.h>   
  20. #include <asm/io.h>   
  21. #include <asm/system.h>   
  22. #include <asm/uaccess.h>   
  23. #include <linux/device.h>       /* device_create() */   
  24.   
  25. static struct class *firstdrv_class;  
  26. static struct class_device *firstdrv_class_dev;  
  27. static int major;  
  28.   
  29. static int first_drv_open(struct inode *inode, struct file *file)  
  30. {  
  31.     printk("First_drv_open\n");  
  32.     return 0;  
  33. }  
  34.   
  35. static ssize_t first_drv_read(struct file *file, const char __user *buf, size_t count, loff_t * ppos)  
  36. {  
  37.     printk("First_drv_read\n");  
  38.     return 0;  
  39. }  
  40.   
  41. static ssize_t first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)  
  42. {  
  43.     printk("First_drv_write\n");  
  44.     return 0;  
  45. }  
  46.   
  47. static int first_drv_release(struct inode *inode, struct file *file)  
  48. {  
  49.     printk("First_drv_release\n");  
  50.     return 0;  
  51. }  
  52.   
  53. static struct file_operations first_drv_fops = {  
  54.     .owner = THIS_MODULE,  
  55.     .open = first_drv_open,  
  56.     .read = first_drv_read,  
  57.     .write = first_drv_write,  
  58.     .release = first_drv_release,  
  59. };  
  60.   
  61. static int __init first_drv_init(void)  
  62. {  
  63.     /* cat /proc/devices  */  
  64.     major = register_chrdev(0, "first_drv_proc", &first_drv_fops);    
  65.       
  66.     /* ls /sys/class/first_drv */  
  67.     firstdrv_class = class_create(THIS_MODULE, "first_drv_sys");      
  68.     if (IS_ERR(firstdrv_class))  
  69.         return PTR_ERR(firstdrv_class);  
  70.   
  71.     /* ls /dev/first_drv */  
  72.     firstdrv_class_dev = device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "first_drv_dev");     
  73.     if (unlikely(IS_ERR(firstdrv_class_dev)))  
  74.         return PTR_ERR(firstdrv_class_dev);  
  75.   
  76.     return 0;  
  77. }  
  78.   
  79. static void __exit first_drv_exit(void)  
  80. {  
  81.     unregister_chrdev(major, "first_drv_proc");  
  82.     device_unregister(firstdrv_class_dev);  
  83.     class_destroy(firstdrv_class);  
  84. }  
  85.   
  86. module_init(first_drv_init);  
  87. module_exit(first_drv_exit);  
  88.   
  89. MODULE_AUTHOR("WHUT-ShiGuang");  
  90. MODULE_DESCRIPTION("Mini2440 Test Driver");  
  91. MODULE_VERSION("1.0");  
  92. MODULE_LICENSE("GPL");  

测试程序:
  1. /************************************************* 
  2. * File name   :  
  3. * Description :  
  4. * Author      : sg131971@qq.com 
  5. * Version     : V1.0 
  6. * Date        :  
  7. * Compiler    : arm-linux-gcc-4.4.3 
  8. * Target      : mini2440(Linux-2.6.32) 
  9. * History     :  
  10. *   <author>  <time>   <version >   <desc> 
  11. *************************************************/  
  12. #include<stdio.h>   
  13. #include<stdlib.h>   
  14. #include<fcntl.h>   
  15. #include<sys/types.h>   
  16. #include<sys/stat.h>   
  17.   
  18. int main()  
  19. {  
  20.     int fd;  
  21.     int val = 1;  
  22.     fd = open("/dev/first_drv_dev", O_RDWR);  
  23.     printf("fd = %d\n", fd);  
  24.     write(fd, &val, 4);  
  25.     read(fd, &val, 4);  
  26.     close(fd);  
  27.     return 0;  
  28. }  
测试结果:

[root@ www.bkjia.com /home]# 
[root@ www.bkjia.com /home]# ls /dev/first_drv_dev -l
crw-rw----    1 root     root     253,   0 Jan  1 08:31 /dev/first_drv_dev
[root@ www.bkjia.com /home]# 
[root@ www.bkjia.com /home]# cat /proc/devices | grep first_drv    
253 first_drv_proc
[root@ www.bkjia.com /home]# 
[root@ www.bkjia.com /home]# ls /sys/class/first_drv_sys/
first_drv_dev
[root@ www.bkjia.com /home]# 
[root@ www.bkjia.com /home]# 
[root@ www.bkjia.com /home]# ./app 
First_drv_open
fd = 3
First_drv_write
First_drv_read
First_drv_release
[root@ www.bkjia.com /home]# 
[root@ www.bkjia.com /home]# 

微笑 OVER! THANK YOU !

相关内容